我有一个文件,比如说C:\source.dat
。我想将其压缩为 zip 文件 C:\folder\destination.zip
。
我找不到一个简单的例子,Maven 项目提供的 HelloWorld 并不真正适用于我的情况,因为我没有编写纯文本文件,所以希望有人能启发我。
示例中提供的代码仅供引用:
@Override
protected int work(String[] args) throws IOException {
// By default, ZIP files use character set IBM437 to encode entry names
// whereas JAR files use UTF-8.
// This can be changed by configuring the respective archive driver,
// see Javadoc for TApplication.setup().
final Writer writer = new TFileWriter(
new TFile("archive.zip/dir/HälloWörld.txt"));
try {
writer.write("Hello world!\n");
} finally {
writer.close();
}
return 0;
}
最佳答案
就这么简单:
new TFile("source.dat").cp(new TFile("destination.zip/source.dat"));
有关详细信息,请参阅 TFile 类的 Javadoc http://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TFile.html .
您可能还想尝试 TrueZIP 原型(prototype)文件*,该文件在 http://truezip.java.net/kick-start/index.html 中介绍。 。该原型(prototype)生成了许多示例程序,您应该探索它们以了解 API。
关于java - 如何使用TrueZip压缩文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096784/