<commit_before>package cn.com.restarter.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by zhengjianwen on 14-2-14.
 */
public class FileUtils {

    //·ļ
    public static HttpServletResponse downLoadFilesByUrl(HttpServletRequest request, HttpServletResponse response,String url,String zipname) throws Exception {
        List<File> files = new ArrayList<File>();
        File file=new File(url);
        File[] files1=file.listFiles();
        for(int i=0;i<files1.length;i++)
        {
            files.add(files1[i]);
        }
        return  downLoadFiles(files,zipname,request,response);
    }

    //ļ
    public static HttpServletResponse downLoadFiles(List<File> files, String zipname,
                                                    HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            /**ϾҪļ
             * Ѿ׼Ҫļ*/
            //List<File> files = new ArrayList<File>();

            /**һʱѹļ
             * ǻļȫע뵽ļ
             * ļԶ.rar.zip*/
            File file = new File("c:/"+zipname);
            if (!file.exists()){
                file.createNewFile();
            }
            response.reset();
            //response.getWriter()
            //ļ
            FileOutputStream fous = new FileOutputStream(file);
            /**ķǻõZipOutputStreamһ,
             * ǰתһ*/
            ZipOutputStream zipOut
                    = new ZipOutputStream(fous);
            /**ܵľһҪļļϣ
             * һZipOutputStream*/
            zipFile(files, zipOut);
            zipOut.close();
            fous.close();
            return downloadZip(file,response);
        }catch (Exception e) {
            e.printStackTrace();
        }
        /**ֱļĴѾɹˣ
         * ļĴ̱ҷװFileUtil.zipFile̬У
         * Ժֳľͻд*/
        // OutputStream out = response.getOutputStream();


        return response ;
    }

    /**
     * ѽܵȫļѹ
     */
    public static void zipFile
    (List files,ZipOutputStream outputStream) {
        int size = files.size();
        for(int i = 0; i < size; i++) {
            File file = (File) files.get(i);
            zipFile(file, outputStream);
        }
    }

    public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
        try {
            // ʽļ
            InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // response
            response.reset();

            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }finally{
            try {
                File f = new File(file.getPath());
                f.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    /**
     * ļļд
     * @param File
     * @param org.apache.tools.zip.ZipOutputStream
     */
    public static void zipFile(File inputFile,
                               ZipOutputStream ouputStream) {
        try {
            if(inputFile.exists()) {
                /**Ŀ¼Ļǲȡģ
                 * Ŀ¼Ĵо*/
                if (inputFile.isFile()) {
                    FileInputStream IN = new FileInputStream(inputFile);
                    BufferedInputStream bins = new BufferedInputStream(IN, 512);
                    //org.apache.tools.zip.ZipEntry
                    ZipEntry entry = new ZipEntry(inputFile.getName());
                    ouputStream.putNextEntry(entry);
                    // ѹļ
                    int nNumber;
                    byte[] buffer = new byte[512];<commit_msg>222222222222222222222222222222222 xx<commit_after>package cn.com.restarter.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by zhengjianwen on 14-2-14.
 */
public class FileUtils {

    //·ļ
    public static HttpServletResponse downLoadFilesByUrl(HttpServletRequest request, HttpServletResponse response, String url, String zipname) throws Exception {
        List<File> files = new ArrayList<File>();
        File file = new File(url);
        File[] files1 = file.listFiles();
        for (int i = 0; i < files1.length; i++) {
            files.add(files1[i]);
        }
        return downLoadFiles(files, zipname, request, response);
    }

    //ļ
    public static HttpServletResponse downLoadFiles(List<File> files, String zipname,
                                                    HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            /**ϾҪļ
             * Ѿ׼Ҫļ*/
            //List<File> files = new ArrayList<File>();

            /**һʱѹļ
             * ǻļȫע뵽ļ
             * ļԶ.rar.zip*/
            File file = new File("c:/" + zipname);
            if (!file.exists()) {
                file.createNewFile();
            }
            response.reset();
            //response.getWriter()
            //ļ
            FileOutputStream fous = new FileOutputStream(file);
            /**ķǻõZipOutputStreamһ,
             * ǰתһ*/
            ZipOutputStream zipOut
                    = new ZipOutputStream(fous);
            /**ܵľһҪļļϣ
             * һZipOutputStream*/
            zipFile(files, zipOut);
            zipOut.close();
            fous.close();
            return downloadZip(file, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**ֱļĴѾɹˣ
         * ļĴ̱ҷװFileUtil.zipFile̬У
         * Ժֳľͻд*/
        // OutputStream out = response.getOutputStream();


        return response;
    }

    /**
     * ѽܵȫļѹ
     */
    public static void zipFile
    (List files, ZipOutputStream outputStream) {
        int size = files.size();
        for (int i = 0; i < size; i++) {
            File file = (File) files.get(i);
            zipFile(file, outputStream);
        }
    }

    public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
        try {
            // ʽļ
            InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // response
            response.reset();

            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                File f = new File(file.getPath());
                f.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    /**
     * ļļд
     */
    public static void zipFile(File inputFile,
                               ZipOutputStream ouputStream) {
        try {
            if (inputFile.exists()) {
                /**Ŀ¼Ļǲȡģ
                 * Ŀ¼Ĵо*/
                if (inputFile.isFile()) {
                    FileInputStream IN = new FileInputStream(inputFile);
                    BufferedInputStream bins = new BufferedInputStream(IN, 512);
                    //org.apache.tools.zip.ZipEntry
                    ZipEntry entry = new ZipEntry(inputFile.getName());
                    ouputStream.putNextEntry(entry);
                    // ѹļ
                    int nNumber;
                    byte[] buffer = new byte[512];
{'fraction_non_alphanumeric': 0.10830152671755726, 'fraction_numerical': 0.007752862595419847, 'mean_word_length': 1.5855689176688252, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4625418', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2281, 'n_words': 466}