【发布时间】:2019-10-15 05:42:10
【问题描述】:
我想在 Java 中将手动定义的字符串压缩为 7z。那么我可以将它转换为base64。我发现了很多将文件压缩到 7z 然后保存到新文件中的示例。
我只是尝试下一个代码,它正确地获取文件并压缩它:
private static void addToArchiveCompression(SevenZOutputFile out, File file, String dir) throws IOException {
String name = dir + File.separator + file.getName();
if (file.isFile()){
SevenZArchiveEntry entry = out.createArchiveEntry(file, name);
out.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int count = 0;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.closeArchiveEntry();
} 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");
}
}
但是如何将手动定义的 String 压缩为 7z 并将其转换为 byte[]?那么我可以将 byte[] 转换为 base64 并打印它,而无需生成或读取新文件?
【问题讨论】:
-
你能分享一下将定义的String压缩成7z并转换成字节数组的代码吗?