大文件想要传输成功,怎么把ZIP文件分卷压缩

不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?

zip

方法一:使用7-Zip免费且强大的文件管理工具

7-Zip也是一款备受欢迎的压缩工具,它完全免费且开源,同样支持文件分割功能。以下是使用7-Zip分割ZIP包的方法:

1、安装并打开7-Zip:首先,从官方网站下载并安装7-Zip。安装完成后,通过右键菜单或直接打开程序来使用它。

2、添加文件到压缩包:右键点击你想要压缩并分割的文件或文件夹,选择“7-Zip” > “添加到压缩包...”。

添加压缩包

3、配置分割选项:在弹出的压缩设置窗口中,设置压缩格式(通常为ZIP),然后点击“分割成卷,大小”选项,并在弹出的输入框中输入你希望每个分卷的大小(支持KB、MB、GB等单位)。你也可以使用下拉菜单选择预设的分割大小。最后设置一个密码,点击确定即可开始进行分卷压缩。

配置分割

这样压缩之后就会出现多个大小相同并且密码相同的压缩包文件

分割

最后需要大家设置了加密的7z文件一定要将密码记牢,防止无法解压文件,但是如果我们忘记了密码,也可以借助工具帮助我们找回密码,比如PassFab for ZIP,提供了三种方法帮助我们找回密码。

zip密码找回工具

### 回答1: 以下是使用 Apache Commons Compress 库实现zip文件分卷压缩的示例代码: ```java import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; public class ZipUtils { public static void zipSplit(File inputFile, int splitSize) throws IOException { BufferedInputStream bis = null; FileInputStream fis = null; ZipArchiveOutputStream zos = null; try { fis = new FileInputStream(inputFile); bis = new BufferedInputStream(fis); byte[] buffer = new byte[1024]; int read = 0; int partNum = 0; while ((read = bis.read(buffer)) != -1) { String partName = inputFile.getName() + ".part" + partNum + ".zip"; File partFile = new File(inputFile.getParentFile(), partName); ZipArchiveEntry zipEntry = new ZipArchiveEntry(inputFile.getName()); zipEntry.setSize(read); zos = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(partFile))); zos.putArchiveEntry(zipEntry); zos.write(buffer, 0, read); zos.closeArchiveEntry(); zos.close(); partNum++; } } finally { IOUtils.closeQuietly(bis); IOUtils.closeQuietly(fis); IOUtils.closeQuietly(zos); } } } ``` 此示例使用 `ZipArchiveOutputStream` 类创建分卷 zip 文件,并将输入文件分割成固定大小的块。在此示例中,文件将被分割成大小为 `splitSize` 的块。 示例中的 `zipSplit` 方法接受两个参数:`inputFile`,它是要分卷压缩文件,以及 `splitSize`,它是每个分卷文件的大小。 请注意,此示例仅创建 zip 文件分卷,而不进行解压缩或合并。如果要解压或合并分卷 zip 文件,请使用 `ZipArchiveInputStream` 类。 ### 回答2: Apache Commons Compress 是一个用于处理各种压缩和存档格式的 Java 库。它提供了一系列实用程序和类,用于创建、读取和操作各种压缩格式的文件。 下面是一个使用 Apache Commons Compress 实现 zip 文件分卷压缩的代码示例: ```java import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.io.*; public class ZipVolumeExample { private static final int CHUNK_SIZE = 1024 * 1024; // 每个分卷的大小设置为1MB public static void main(String[] args) { String sourceFilePath = "path/to/source/file.txt"; String destinationFolderPath = "path/to/destination/folder"; String destinationBaseFileName = "output"; File sourceFile = new File(sourceFilePath); File destinationFolder = new File(destinationFolderPath); if (!sourceFile.exists()) { System.out.println("Source file does not exist!"); return; } if (!destinationFolder.exists() || !destinationFolder.isDirectory()) { System.out.println("Destination folder does not exist!"); return; } try (InputStream inputStream = new FileInputStream(sourceFile)) { int volumeCount = 1; int bytesRead; byte[] buffer = new byte[CHUNK_SIZE]; ZipArchiveOutputStream zipOutputStream = null; while ((bytesRead = inputStream.read(buffer)) > 0) { String volumeFileName = String.format("%s_%03d.zip", destinationBaseFileName, volumeCount++); File volumeFile = new File(destinationFolder, volumeFileName); zipOutputStream = new ZipArchiveOutputStream(new FileOutputStream(volumeFile)); ZipArchiveEntry zipEntry = new ZipArchiveEntry(sourceFile.getName()); zipOutputStream.putArchiveEntry(zipEntry); zipOutputStream.write(buffer, 0, bytesRead); zipOutputStream.closeArchiveEntry(); zipOutputStream.close(); } if (zipOutputStream != null) { zipOutputStream.close(); } System.out.println("Zip volume compression completed!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码示例中,我们通过指定源文件路径和目标文件夹路径来定义要进行分卷压缩文件以及输出文件夹。代码中的 CHUNK_SIZE 变量定义了每个分卷的大小,这里设置为 1MB。然后,我们通过循环读取源文件,并将数据写入分卷zip 文件中。 在循环过程中,我们使用 ZipArchiveOutputStream 类创建一个新的 zip 输出流,并将源文件的内容写入到该流中。每当达到 CHUNK_SIZE 大小时,我们就关闭当前的分卷 zip 文件,并为下一个分卷创建一个新的 zip 输出流。最后,我们在循环结束后关闭输出流。 通过使用 Apache Commons Compress 提供的 ZipArchiveOutputStream 类,我们能够轻松实现 zip 文件分卷压缩。这种方法可以将大型文件分割成多个块,方便传输和存储。 ### 回答3: Apache Commons Compress是一个开源的Java库,用于处理各种压缩和解压缩格式的文件。它提供了丰富的功能,包括对zip文件的创建、更新、解压缩等操作。以下是一个使用Apache Commons Compress实现zip文件分卷压缩的代码示例: ```java import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ZipSplitExample { public static void main(String[] args) throws IOException { String inputFilePath = "path/to/input/file.zip"; String outputDirectory = "path/to/output/directory/"; int maxFileSize = 10 * 1024 * 1024; // 10 MB File inputFile = new File(inputFilePath); FileInputStream fis = new FileInputStream(inputFile); byte[] buffer = new byte[maxFileSize]; int partNum = 1; int bytesRead; while ((bytesRead = fis.read(buffer)) > 0) { String outputFilePath = outputDirectory + "part" + partNum + ".zip"; FileOutputStream fos = new FileOutputStream(outputFilePath); ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(fos); ZipArchiveEntry entry = new ZipArchiveEntry("part" + partNum + ".bin"); zipOutput.putArchiveEntry(entry); zipOutput.write(buffer, 0, bytesRead); zipOutput.closeArchiveEntry(); zipOutput.finish(); zipOutput.close(); fos.close(); partNum++; } fis.close(); } } ``` 上述代码中,首先需要指定输入的zip文件路径(inputFilePath),以及输出分卷压缩文件的目录(outputDirectory)。参数maxFileSize用于指定每个分卷的最大文件大小(此处设置为10MB)。 代码会从输入zip文件中读取数据,并按照每个分卷的最大文件大小将数据写入到输出文件中。每个分卷文件名通过添加后缀"part"和分卷编号来表示。 需要注意的是,代码中虽然只演示了对zip文件分卷压缩操作,但Apache Commons Compress还提供了丰富的其他功能,如解压缩、添加、删除文件等操作,可以根据具体需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值