Java-添加文件到Zip压缩包

Java-添加文件到Zip压缩包

Tags: Java

摘要

Java-添加文件到Zip压缩包

Java压缩zip包

java通过包java.util.zip里面的相关类,实现压缩zip。大体过程如下

通过FileInputStream读取文件
创建ZipEntry对象,然后写入到ZipOutputStream

例子1 添加单个文件到zip压缩包

package cn.javacore.io.zip;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class _01_CompressZip {
    public static void main(String[] args) {
        byte[] buffer = new byte[1024];

        try {

            FileOutputStream fos = new FileOutputStream("/home/zhu/javacore.cn/MyFile.zip");
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry("abc/test02.log");//压缩包内的路径,可以指定路径及不同的名字
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream("/home/zhu/javacore.cn/test.log");

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
            zos.closeEntry();

            //remember close it
            zos.close();

            System.out.println("Done");

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

输出结果

例子2 添加文件夹到zip压缩包中

读取文件夹/home/zhu/javacore.cn/folder里的所有内容,添加到/home/zhu/javacore.cn/MyFile.zip 压缩包内,因为会递归所有子文件夹(不包括空文件夹),所以压缩包内也会创建文件夹。

package cn.javacore.io.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class _02_CompressZipFolder {
    List<String> fileList;
    private static final String OUTPUT_ZIP_FILE = "/home/zhu/javacore.cn/MyFile.zip";
    private static final String SOURCE_FOLDER = "/home/zhu/javacore.cn/folder";

    _02_CompressZipFolder() {
        fileList = new ArrayList<String>();
    }

    public static void main(String[] args) {
        _02_CompressZipFolder appZip = new _02_CompressZipFolder();
        appZip.generateFileList(new File(SOURCE_FOLDER));
        appZip.zipIt(OUTPUT_ZIP_FILE);
    }

    /**
     * Zip it
     *
     * @param zipFile output ZIP file location
     */
    public void zipIt(String zipFile) {

        byte[] buffer = new byte[1024];

        try {

            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            System.out.println("Output to Zip : " + zipFile);

            for (String file : this.fileList) {

                System.out.println("File Added : " + file);
                ZipEntry ze = new ZipEntry(file);
                zos.putNextEntry(ze);

                FileInputStream in =
                        new FileInputStream(SOURCE_FOLDER + File.separator + file);

                int len;
                while ((len = in.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }

                in.close();
            }

            zos.closeEntry();
            //remember close it
            zos.close();

            System.out.println("Done");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Traverse a directory and get all files,
     * and add the file into fileList
     *
     * @param node file or directory
     */
    public void generateFileList(File node) {

        //add file only
        if (node.isFile()) {
            fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
        }

        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                generateFileList(new File(node, filename));
            }
        }

    }

    /**
     * Format the file path for zip
     *
     * @param file file path
     * @return Formatted file path
     */
    private String generateZipEntry(String file) {
        return file.substring(SOURCE_FOLDER.length() + 1, file.length());
    }
}

压缩文件夹输出结果

Output to Zip : /home/zhu/javacore.cn/MyFile.zip
File Added : test-02.log
File Added : test.log
File Added : sub/ABC-02.log
File Added : sub/abc.log
Done