Java 文件压缩和解压

版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/w18756901575 https://blog.csdn.net/w18756901575/article/details/79026001
package com.wkk.util;

import java.io.*;
import java.util.zip.*;

public class Util {

    /**
     * 压缩文件
     *
     * @param zipPath 文件压缩后生成的文件路径
     * @param f       需要压缩的文件
     * @throws IOException
     */
    public static void compressionFile(String zipPath, File f) throws IOException {
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
        File files[] = new File[]{f};
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                isDirectory(zipOutputStream, file, file.getName());
            } else if (file.isFile()) {
                isFile(zipOutputStream, file, "");
            }
        }
        zipOutputStream.flush();
        zipOutputStream.close();
    }

    private static void isDirectory(ZipOutputStream zipOutputStream, File file, String pathName) throws IOException {
        File[] files = file.listFiles();
        if (files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                File file1 = files[i];
                if (file1.isDirectory()) {
                    isDirectory(zipOutputStream, file1, pathName + File.separator + file1.getName());
                } else if (file1.isFile()) {
                    isFile(zipOutputStream, file1, pathName);
                }
            }
        } else {
            String name = pathName + "/";
            ZipEntry zipEntry = new ZipEntry(name);
            zipOutputStream.putNextEntry(zipEntry);
            zipOutputStream.closeEntry();
        }
    }

    private static void isFile(ZipOutputStream zipOutputStream, File file, String pathName) throws IOException {
        ZipEntry zipEntry = null;
        if (pathName == null || pathName.equalsIgnoreCase("")) {
            zipEntry = new ZipEntry(file.getName());
        } else {
            zipEntry = new ZipEntry(pathName + File.separator + file.getName());
        }
        zipOutputStream.putNextEntry(zipEntry);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] b = new byte[1024];
        int l = 0;
        while ((l = fileInputStream.read(b)) > 0) {
            zipOutputStream.write(b, 0, l);
        }
        fileInputStream.close();
        zipOutputStream.closeEntry();
    }


    /**
     * 解压文件 如果压缩文件中有文件夹将会发生异常
     *
     * @param zipFilePath 压缩文件路径
     * @param unpackPath  解压后文件存放路径
     * @throws IOException
     */
    public static void UnpackFile(String zipFilePath, String unpackPath) throws IOException {
        File file = new File(zipFilePath);
        ZipFile zipFile = new ZipFile(file);
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String path = unpackPath + "\\" + zipEntry.getName();
            new File(path).mkdirs();
            if (zipEntry.isDirectory()) {
                continue;
            }
            new File(path).delete();
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            InputStream inputStream = zipFile.getInputStream(zipEntry);
            byte b[] = new byte[1024];
            int l = 0;
            while ((l = inputStream.read(b)) > 0) {
                fileOutputStream.write(b, 0, l);
            }
            inputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        zipFile.close();
        zipInputStream.close();
    }

}

欢迎交流,与君共勉

猜你喜欢

转载自blog.csdn.net/w18756901575/article/details/79026001
今日推荐