《Java知识应用》Java压缩文件和解压缩

 今天通过Java实现一下:文件的压缩和解压缩。

1. 压缩

准备文件:

准备代码:(压缩)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressUtils {

    public static void main(String[] args) {
        //需要压缩的文件路径
        List<String> filePathList = new ArrayList<>();
        filePathList.add("src\\demo\\knowledgepoints\\compress\\filezip\\1.docx");
        filePathList.add("src\\demo\\knowledgepoints\\compress\\filezip\\1.pdf");

        //压缩后的文件路径
        String zipPath = "src/demo/knowledgepoints/compress/filezip/1.zip";
        toZip(filePathList,zipPath);
    }

    /**
     * 压缩文件
     * @param filePathList
     * @param zipPath
     * @throws RuntimeException
     */
    public static void toZip(List<String> filePathList,String zipPath) throws RuntimeException {
        List<File> adjustFiles = new ArrayList<>();
        for (String filePath :filePathList){
            adjustFiles.add(new File(filePath));
        }

        ByteArrayOutputStream fos2 = new ByteArrayOutputStream();
        CompressUtils.toZip(adjustFiles, fos2);

        BufferedOutputStream output = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        byte[] fileBytes = fos2.toByteArray();
        try {
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(fileBytes);

            bis = new BufferedInputStream(byteInputStream);
            File file = new File(zipPath);
            file.createNewFile();

            fos = new FileOutputStream(file);
            // 实例化OutputString 对象
            output = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                output.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (output != null) {
                    output.close();
                }
            } catch (IOException e0) {
                e0.printStackTrace();
            }
        }
    }

    /**
     * 压缩成ZIP
     * @param srcFiles 需要压缩的文件列表
     * @param out      压缩文件输出流
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[1024];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

运行效果:

 

1. 解压缩

将刚刚压缩的文件放到新的路径下:

准备代码:(解压缩)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class CompressUtils {

    public static void main(String[] args) {
        String zipPath = "src\\demo\\knowledgepoints\\compress\\file\\1.zip";
        String filePath = "src\\demo\\knowledgepoints\\compress\\file";
        zipToFile(zipPath,filePath);
    }

    /**
     * zip解压过程
     * @param zipPath  压缩文件路径
     * @param filePath 解压后文件夹路径
     * @throws RuntimeException
     */
    public static void zipToFile(String zipPath, String filePath) throws RuntimeException {
        long startTime = System.currentTimeMillis();

        BufferedInputStream Bin = null;  //数据源缓存流
        ZipInputStream Zin = null;  //数据源
        FileOutputStream out = null;  //输出流
        BufferedOutputStream Bout = null;  //输出缓存流
        try {
            Zin = new ZipInputStream(new FileInputStream(zipPath));//输入源zip路径
            Bin = new BufferedInputStream(Zin);
            File Fout;
            ZipEntry entry;
            while ((entry = Zin.getNextEntry()) != null && !entry.isDirectory()) {
                Fout = new File(filePath, entry.getName());
                if (!Fout.exists()) {
                    new File(Fout.getParent()).mkdirs();
                }
                out = new FileOutputStream(Fout);
                Bout = new BufferedOutputStream(out);
                int bytes;
                while ((bytes = Bin.read()) != -1) {
                    Bout.write(bytes);
                }
                System.out.println(Fout + "解压成功");
            }
            long endTime = System.currentTimeMillis();
            System.out.println("耗费时间: " + (endTime - startTime) + " ms");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (Bin != null) {
                    Bin.close();
                }
                if (Zin != null) {
                    Zin.close();
                }
                if (Bout != null) {
                    Bout.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果:

参考:https://www.cnblogs.com/wangxuemei/p/8360800.html

 

 

 

posted @ 2019-11-14 22:04  加速丨世界  阅读(322)  评论(0编辑  收藏  举报