Comparison of several ways of decompressing files with Java code

Comparison of several kinds of Java code compressed files

Compression format Source file size Compressed size Compression time (ms) Decompression time (ms)
- 100kb 2kb 5-9 2-15
zip 1M 6kb 10-20 10-20
- 10M 56kb 100-160 30-100
- 100kb 2kb 30 30-40
tar.gz 1M 6kb 40-50 80-100
- 10M 56kb 200-240 400-600
- 100kb 2kb 5-9 -
rar 1M 6kb 10-20 -
- 10M 56kb 130 -
package com.atguigu.springboot.util;

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.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 文件的压缩,解压工具类
 */
public class ZipUtils {
    
    
	
	/**
	 * 示例:
	 * 测试解压缩的方法,加上时间戳,测试开始时间和结束时间,以及方法执行的时间
	 * 异常和关键信息的打印,可以根据自己框架的日志工厂来处理
	 */
	public void testFile() {
    
    
		//时间格式化,精确到毫秒
		SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss:SSS");
		 try {
    
    
			 Date beforeTime = new Date();
			 String before_time = sf.format(beforeTime);
			 System.out.println("程序开始的时间:-------"+before_time);
			//ZipCompress("D:\\result.txt", "D:\\result.zip");
			 ZipUncompress("D:\\zipceshi.zip","D:\\ttt\\1_result.txt");
			Date afterTime = new Date();
			String after_time = sf.format(afterTime);
			System.out.println("程序结束的时间:-------"+after_time);
		} catch (Exception e) {
    
    
			System.out.println("压缩或解压方法发生异常-----");
			e.printStackTrace();
		}
	}
	
	 /**
     * zip文件压缩
     * @param inputFile 待压缩文件夹/文件名
     * @param outputFile 生成的压缩包名字
     */
	  public static void ZipCompress(String inputFile, String outputFile) throws Exception {
    
    
	        //创建zip输出流
	        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
	        //创建缓冲输出流
	        BufferedOutputStream bos = new BufferedOutputStream(out);
	        File input = new File(inputFile);
	        compress(out, bos, input,null);
	        bos.close();
	        out.close();
	    }
	  
	  
	  /**
	     * @param name 压缩文件名,可以写为null保持默认
	     */
	    //递归压缩
	    public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {
    
    
	        if (name == null) {
    
    
	            name = input.getName();
	        }
	        //如果路径为目录(文件夹)
	        if (input.isDirectory()) {
    
    
	            //取出文件夹中的文件(或子文件夹)
	            File[] flist = input.listFiles();

	            if (flist.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入
	            {
    
    
	                out.putNextEntry(new ZipEntry(name + "/"));
	            } else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
	            {
    
    
	                for (int i = 0; i < flist.length; i++) {
    
    
	                    compress(out, bos, flist[i], name + "/" + flist[i].getName());
	                }
	            }
	        } else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
	        {
    
    
	            out.putNextEntry(new ZipEntry(name));
	            FileInputStream fos = new FileInputStream(input);
	            BufferedInputStream bis = new BufferedInputStream(fos);
	            int len=-1;
	            //将源文件写入到zip文件中
	            byte[] buf = new byte[1024];
	            while ((len = bis.read(buf)) != -1) {
    
    
	                bos.write(buf,0,len);
	            }
	            bis.close();
	            fos.close();
	        }
	    }
	  
	  
	    
	    /**
	     * zip解压
	     * @param inputFile 待解压文件名
	     * @param destDirPath  解压路径
	     */

	    public static void ZipUncompress(String inputFile,String destDirPath) throws Exception {
    
    
	        File srcFile = new File(inputFile);//获取当前压缩文件
	        // 判断源文件是否存在
	        if (!srcFile.exists()) {
    
    
	            throw new Exception(srcFile.getPath() + "所指文件不存在");
	        }
	        //开始解压
	        //构建解压输入流
	        ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile));
	        ZipEntry entry = null;
	        File file = null;
	        while ((entry = zIn.getNextEntry()) != null) {
    
    
	            if (!entry.isDirectory()) {
    
    
	                file = new File(destDirPath, entry.getName());
	                if (!file.exists()) {
    
    
	                    new File(file.getParent()).mkdirs();//创建此文件的上级目录
	                }
	                OutputStream out = new FileOutputStream(file);
	                BufferedOutputStream bos = new BufferedOutputStream(out);
	                int len = -1;
	                byte[] buf = new byte[1024];
	                while ((len = zIn.read(buf)) != -1) {
    
    
	                    bos.write(buf, 0, len);
	                }
	                // 关流顺序,先打开的后关闭
	                bos.close();
	                out.close();
	            }
	        }
	    } 
	    
	    


}

Guess you like

Origin blog.csdn.net/qq_38220334/article/details/113184174