gpt4 book ai didi

java - 如何使用 lzma 压缩创建 zip

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:58 24 4
gpt4 key购买 nike

我知道如何创建 zip 存档:

import java.io.*;
import java.util.zip.*;
public class ZipCreateExample{
public static void main(String[] args) throws Exception
// input file
FileInputStream in = new FileInputStream("F:/sometxt.txt");

// out put file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip"));

// name the file inside the zip file
out.putNextEntry(new ZipEntry("zippedjava.txt"));

// buffer size
byte[] b = new byte[1024];
int count;

while ((count = in.read(b)) > 0) {
System.out.println();
out.write(b, 0, count);
}
out.close();
in.close();
}
}

但是我不知道如何使用lzma压缩。

我找到了这个项目:https://github.com/jponge/lzma-java哪个创建压缩文件,但我不知道如何将它与我现有的解决方案结合起来。

最佳答案

Apache Commons Compress 的最新版本(2013 年 10 月 23 日发布的 1.6)支持 LZMA 压缩。

看看http://commons.apache.org/proper/commons-compress/examples.html ,特别是关于 .7z 压缩/解压缩的那个。

例如,你想从 HTTP 响应中存储一个 html 页面,并且你想压缩它:

SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("outFile.7z"));

File entryFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "web.html");
SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(entryFile, "web.html");

sevenZOutput.putArchiveEntry(entry);
sevenZOutput.write(rawHtml.getBytes());
sevenZOutput.closeArchiveEntry();
sevenZOutput.close();

关于java - 如何使用 lzma 压缩创建 zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com