java 打包的两种方式

一.利用java 自带的util.zip 将多个文件打包在一个文件夹之中

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZip {
    //将所有的文件打包到一个zip包中 没有目录结构 且运用的是jdk自带的打包工具
    public static void main(String[] args) {
        //要打包的文件路径
        List<String> listKey = new ArrayList<String>();
        listKey.add("D:\img\1.png");
        listKey.add("D:\img\2.png");
        listKey.add("D:\img\论文解析详细设计.docx");
        File zipFile = new File("D:\zipTest\img.zip");// 最终打包的压缩包
        System.out.println("zipFile exists: " + zipFile.exists());
        System.out.println("zipFile size: " + zipFile.length());
        packageZip(zipFile,listKey);
        System.out.println("zipFile exists2: " + zipFile.exists());
        System.out.println("zipFile size: " + zipFile.length());

    }
    public static boolean packageZip(File zipFile, List<String> listKey){
        //图片打包操作
        ZipOutputStream zipStream = null;
        FileInputStream zipSource = null;
        BufferedInputStream bufferStream = null;
        try {
            zipStream = new ZipOutputStream(new FileOutputStream(zipFile));// 用这个构造最终压缩包的输出流
            for (String picKey : listKey) {
                File file = new File(picKey);
                zipSource = new FileInputStream(file);
                byte[] bufferArea = new byte[1024 * 10];// 读写缓冲区
                // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
                ZipEntry zipEntry = new ZipEntry(file.getName());
                zipStream.putNextEntry(zipEntry);// 定位到该压缩条目位置,开始写入文件到压缩包中
                bufferStream = new BufferedInputStream(zipSource, 1024 * 10);// 输入缓冲流
                int read = 0;
                // 在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1]
                // 的元素都不会受到影响。这个是官方API给出的read方法说明,经典!
                while ((read = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
                    zipStream.write(bufferArea, 0, read);
                }
            }
        } catch (Exception e) {
            return false;
        } finally {
            // 关闭流
            try {
                if (null != bufferStream)
                    bufferStream.close();
                if (null != zipStream)
                    zipStream.close();
                if (null != zipSource)
                    zipSource.close();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}

二,采用apache的ant打包,包含文件夹一起打包

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class CreateZipAnt {


    public static void main(String[] args) {
        try {
            long starTime = 0;
            long endTime = 0;
            System.out.println("开始压缩...");
            starTime = System.currentTimeMillis();
            ZIP("D:\img", "D:\zipTest\img.zip");
            endTime = System.currentTimeMillis();
            System.out.println("压缩完毕!花费时间: " + (endTime - starTime) + " 毫秒!");

        } catch (Exception ex) {
            ex.printStackTrace();
        }


    }

    public static void ZIPDIR(String sourceDir, ZipOutputStream zos, String tager) throws IOException {
        //System.out.println(tager);
        ZipEntry ze = new ZipEntry(tager);
        zos.putNextEntry(ze);
        // 提取要压缩的文件夹中的所有文件
        File f = new File(sourceDir);
        File[] flist = f.listFiles();
        if (flist != null) {
            // 如果该文件夹下有文件则提取所有的文件进行压缩
            for (File fsub : flist) {
                if (fsub.isDirectory()) {
                    //如果是目录则进行目录压缩
                    ZIPDIR(fsub.getPath(), zos, tager + fsub.getName() + "/");
                } else {
                    //如果是文件,则进行文件压缩
                    ZIPFile(fsub.getPath(), zos, tager + fsub.getName());
                }
            }
        }
    }

    public static void ZIP(String source, String zipFileName) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new File(zipFileName));

        // 设置压缩的时候文件名编码为gb2312
        zos.setEncoding("gb2312");
        File f = new File(source);
        if (f.isDirectory()) {
            // 如果直接压缩文件夹
            ZIPDIR(source, zos, f.getName() + "/");// 此处使用/来表示目录,如果使用\来表示目录的话,会导致压缩后的文件目录组织形式在解压缩的时候不能正确识别。
        } else {
            // 如果直接压缩文件
            ZIPDIR(f.getPath(), zos, new File(f.getParent()).getName() + "/");
            ZIPFile(f.getPath(), zos, new File(f.getParent()).getName() + "/" + f.getName());
        }

        zos.closeEntry();
        zos.close();
    }

    public static void ZIPFile(String sourceFileName, ZipOutputStream zos, String tager) throws IOException {

        ZipEntry ze = new ZipEntry(tager);
        zos.putNextEntry(ze);

        // 读取要压缩文件并将其添加到压缩文件中
        FileInputStream fis = new FileInputStream(new File(sourceFileName));
        byte[] bf = new byte[2048];
        int location = 0;
        while ((location = fis.read(bf)) != -1) {
            zos.write(bf, 0, location);
        }
        fis.close();
    }
}
原文地址:https://www.cnblogs.com/huangzhimin/p/10641055.html