gpt4 book ai didi

java - 如何在 java 中创建多部分压缩的 zip 文件

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

我需要压缩 zip(tar、gz、7z 等)文件中的目录。没关系,但我需要创建相互连接的多部分 zip 文件(如 file1.part1.zip、file1.part2.zip)

我如何在 java 中创建多部分 zip 文件?每个部分都必须有最大大小限制。

最佳答案

Zip4j支持创建拆分 zip 文件。这是创建拆分 zip 文件的示例(示例取自 Zip4j examples package)

ZipFile.createZipFile(File sourceFile, ZipParameters parameters, boolean splitArchive, long splitLength) 

是创建拆分 zip 文件的方法。在这种情况下,boolean splitArchive 必须设置为 true。您可以通过 long splitLength

设置每个拆分文件(z01、z02 等)的最大文件大小
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 - 如何在 java 中创建多部分压缩的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18590362/

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