如何在Java中创建多部分压缩zip文件 [英] How can I create multipart compressed zip file in java
问题描述
我需要压缩zip(tar,gz,7z等)文件中的目录.可以,但是我需要创建相互连接的多部分zip文件(例如file1.part1.zip,file1.part2.zip)
I need to compress a directory inside a zip (tar, gz, 7z, etc.) file. It's ok but I need to create multipart zip files connected to each other (like file1.part1.zip, file1.part2.zip)
如何在Java中创建多部分zip文件? 每个零件都必须有最大尺寸限制.
How can i create multipart zip file in java? Each part must have a maximum size limit.
推荐答案
Zip4j 支持创建拆分zip文件.这是创建拆分zip文件的示例(示例摘自 Zip4j示例包)
Zip4j supports creation of split zip file. Here's a sample to create a split zip file (Sample taken from Zip4j examples package)
ZipFile.createZipFile(File sourceFile, ZipParameters parameters, boolean splitArchive, long splitLength)
是创建拆分zip文件的方法.在这种情况下,必须将boolean splitArchive
设置为true.您可以通过long splitLength
is the method to create a split zip file. boolean splitArchive
has to be set to true in this case. You can set the maximum file size for each split file (z01, z02, etc) via long splitLength
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
public class CreateSplitZipFile {
public CreateSplitZipFile() {
try {
// Initiate ZipFile object with the path/name of the zip file.
ZipFile zipFile = new ZipFile("c:\\ZipTest\\CreateSplitZipFile.zip");
// Build the list of files to be added in the array list
// Objects of type File have to be added to the ArrayList
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi"));
filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3"));
// Initiate Zip Parameters which define various properties such
// as compression method, etc.
ZipParameters parameters = new ZipParameters();
// set compression method to store compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// Set the compression level. This value has to be in between 0 to 9
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Create a split file by setting splitArchive parameter to true
// and specifying the splitLength. SplitLenth has to be greater than
// 65536 bytes
// Please note: If the zip file already exists, then this method throws an
// exception
zipFile.createZipFile(filesToAdd, parameters, true, 10485760);
} catch (ZipException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new CreateSplitZipFile();
}
}
这篇关于如何在Java中创建多部分压缩zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!