Discuss / Java / 使用ZipInputStream 和 ZipOutputStrean 实现 解压zip文件和压缩zip文件。

使用ZipInputStream 和 ZipOutputStrean 实现 解压zip文件和压缩zip文件。

Topic source
package demo.io.file;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class TestZipFile {
    public static void main(String[] args) throws IOException {
        String sourcePath = "C:\\Users\\25433\\Pictures\\公众号1";
        String targetPath = "C:\\Users\\25433\\Pictures\\公众号.zip";
        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);
        /*if (sourceFile.exists()) {
            if (!targetFile.exists()) {
                targetFile.createNewFile();
            }
            try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)))) {
                zip(sourceFile, zipOutputStream, "");
            }
        }*/
        if (!sourceFile.exists()) {
            sourceFile.mkdirs();
        }
        unzip(targetPath, sourcePath);
    }
    public static void zip(File source, ZipOutputStream zipOutputStream, String path) throws IOException {
        if (source.exists()) {
            if (source.isDirectory()) {
                File[] files = source.listFiles();
                for (File file1 : files) {
                    zip(file1, zipOutputStream, path + File.separator + source.getName());
                }
            } else {
                zipOutputStream.putNextEntry(new ZipEntry(path + File.separator + source.getName()));
                zipOutputStream.write(getData(source));
                zipOutputStream.closeEntry();
            }
        }
    }
    public static void unzip(String sourcePath, String targetPath) throws IOException {
        File source = new File(sourcePath);
        if (source.exists() && source.isFile()) {
            try (ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(source)))) {
                ZipEntry nextEntry;
                while ((nextEntry = zipInputStream.getNextEntry()) != null) {
                    String name = nextEntry.getName();
                    String dir = name.substring(0, name.lastIndexOf(File.separator));
                    String fi = name.substring(name.lastIndexOf(File.separator) + 1, name.length());
                    File file = new File(targetPath + dir);
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    file = new File(file.getAbsolutePath() + File.separator + fi);
                    if (file.createNewFile()) {
                        try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file))) {
                            byte[] buff = new byte[8192];
                            int count;
                            while ((count = zipInputStream.read(buff, 0, 8192)) != -1) {
                                bufferedOutputStream.write(buff, 0, count);
                            }
                            bufferedOutputStream.flush();
                        }
                    }
                }
            }
        }
    }
    public static byte[] getData(File file) throws IOException {
        byte[] buff = new byte[0];
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
            buff = new byte[bufferedInputStream.available()];
            bufferedInputStream.read(buff);
        }
        return buff;
    }
}

  • 1

Reply