JAVA 7z Seven Zip 压缩和解压文件

JAVA 7z Seven Zip 压缩和解压文件

7-Zip是基于GNU LGPL协议发布的软件,通过全新算法使压缩比率大幅提升
本文主要讲解通过JAVA方式把文件压缩成7z文件和对7z文件进行解压操作

1 Maven依赖项:

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-compress</artifactId>
	<version>1.19</version>
</dependency>
<dependency>
    <groupId>org.tukaani</groupId>
    <artifactId>xz</artifactId>
    <version>1.8</version>
</dependency>

2 递归压缩和解压缩7z文件

压缩:使用SevenZOutputFile将文件或目录压缩为7z格式。
解压:使用SevenZFile类读取7z文件,然后,我们使用sevenZFile.getNextEntry()类循环遍历SevenZArchiveEntry,并将内容写入FileOutputStream。

package cn.rxzn.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;

/**
* @Description: 7Z工具类
* @author qinglj
**/
public class SevenZip {
	
	private SevenZip() {}

	/**
	 *7Z 压缩
	 * @param name 压缩后的文件路径(如 D:\SevenZip\test.7z)
	 * @param files 需要压缩的文件
	 */
    public static void compress(String name, File... files) {
        try (
        	SevenZOutputFile out = new SevenZOutputFile(new File(name))){
            for (File file : files){
                addToArchiveCompression(out, file, ".");
            }
        } catch (IOException e) {
			e.printStackTrace();
		}
    }

    /**
     * 解压7z文件
     * @param orgPath 源压缩文件地址
     * @param tarpath 解压后存放的目录地址
     */
    public static void decompress(String orgPath, String tarpath) {
        try {
            SevenZFile sevenZFile = new SevenZFile(new File(orgPath));
            SevenZArchiveEntry entry = sevenZFile.getNextEntry();
            while (entry != null) {
            	File file = new File(tarpath + File.separator + entry.getName());
                if (entry.isDirectory()) {
                    if(!file.exists()) {
                    	file.mkdirs();
                    }
                    entry = sevenZFile.getNextEntry();
                    continue;
                }
                if(!file.getParentFile().exists()) {
                	file.getParentFile().mkdirs();
                }
                FileOutputStream out = new FileOutputStream(file);
                byte[] content = new byte[(int) entry.getSize()];
                sevenZFile.read(content, 0, content.length);
                out.write(content);
                out.close();
                entry = sevenZFile.getNextEntry();
            }
            sevenZFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void addToArchiveCompression(SevenZOutputFile out, File file, String dir) {
    	String name = dir + File.separator + file.getName();
    	if(dir.equals(".")) {
    		name = file.getName();
    	}
        if (file.isFile()){
        	SevenZArchiveEntry entry = null;
        	FileInputStream in = null;
        	try {
				entry = out.createArchiveEntry(file, name);
				out.putArchiveEntry(entry);
	            in = new FileInputStream(file);
	            byte[] b = new byte[1024];
	            int count = 0;
	            while ((count = in.read(b)) > 0) {
	                out.write(b, 0, count);
	            }
        	} catch (IOException e) {
        		e.printStackTrace();
        	} finally {
        		try {
					out.closeArchiveEntry();
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        		
        	}
        } else if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children != null){
                for (File child : children){
                    addToArchiveCompression(out, child, name);
                }
            }
        } else {
            System.out.println(file.getName() + " is not supported");
        }
    }
}

3 测试

测试代码如下,发现 7z 压缩速度确实比 zip 慢,但使用 7z 格式能比使用 zip 格式的压缩文件小 30-70%。

public static void main(String[] args) {
    System.out.println("压缩开始:" + System.currentTimeMillis());
	compress("D:\\Download\test.7z", new File("D:\\Download\\7z"));
    // decompress("D:\\Download\\test.7z", "D:\\Download\\new7z");
	System.out.println("压缩完成:" + System.currentTimeMillis());
}

4 参考资料
Java 7z Seven Zip Example – compress and decompress a file

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值