文件或文件夹压缩工具类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
 * 
 * 实现将文件或文件夹压缩为zip格式的文件
 * @author 彭锋
 * @2018年5月19日 下午3:49:04
 */
public class ZipUtils {
    /**
     * 设置压缩过程中字节数组的大小
     */
    private static final int BUFFER_SIZE = 1024*1024;

    /**
     * 压缩多个文件或文件夹,到指定输出流中
     * @param srcFiles
     *            需要压缩的文件列表
     * @param out
     *            压缩文件输出流
     */
    public static void toZip(OutputStream out,String... filePaths) {
        try (ZipOutputStream zos = new ZipOutputStream(out)) {
            long start = System.currentTimeMillis();
            for (String srcFile : filePaths) {
                compress(new File(srcFile), zos);
            }
            System.out.println("压缩完成,耗时:" + (System.currentTimeMillis() - start) + " ms");
        } catch (Exception e) {
            System.err.println("压缩失败!");
            e.printStackTrace();
        }
    }
    /**
     * 
     * 使用文件队列的方式压缩文件和文件夹
     * @param file
     *            源文件
     * @param zos
     *            zip输出流
     */
    private static void compress(File file,ZipOutputStream zos) {
        String rootPath = file.getAbsolutePath();
        String rootName = file.getName();
        byte[] byts = new byte[BUFFER_SIZE];
        LinkedList<File> list = new LinkedList<>();
        list.addFirst(file);
        while(!list.isEmpty()) {
            File srcFile = list.removeLast();
            String name = srcFile.getAbsolutePath().replace(rootPath, rootName);
            if (srcFile.isFile()) {
                try (FileInputStream input = new FileInputStream(srcFile);) {
                    // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
                    zos.putNextEntry(new ZipEntry(name));
                    int len = 0;
                    while ((len = input.read(byts)) != -1) {
                        zos.write(byts, 0, len);
                    }
                    zos.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                File[] listFiles = srcFile.listFiles();
                if (listFiles == null || listFiles.length == 0) {
                    // 空文件夹的处理
                    try {
                        zos.putNextEntry(new ZipEntry(name + "/"));
                        // 没有文件,不需要文件的copy
                        zos.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    for (File sonFile : listFiles) {
                        list.addFirst(sonFile);
                    }
                }
            }
        }
    }
    /**
     * 测试压缩方法
     */
    public static void main(String[] args) throws Exception {
        FileOutputStream fos2 = new FileOutputStream(new File("F:\\pic\\2.zip"));
        ZipUtils.toZip(fos2,"F:\\pic\\1.jpg","F:\\pic\\2.jpg");
    }
}

猜你喜欢

转载自www.cnblogs.com/pf1988/p/9060620.html