java 分卷压缩_Java:分卷压缩和解压缩请选择Zip4j

本文展示了如何在Java中使用Zip4j库创建分卷压缩文件。通过示例代码,演示了如何初始化ZipFile对象,设置压缩参数,添加文件到压缩列表,并创建分卷大小为10MB的压缩文件。
摘要由CSDN通过智能技术生成

外国网站,访问困难,贴出主要的代码在下面:

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值