How to create multi-line compressed zip file in java

I need to compress the directory inside the zip file (tar, gz, 7z, etc.). This is fine, but I need to create multi-line zip files linked to each other (for example file1.part1.zip, file1.part2.zip)

How to create a multi-line zip file in java? Each piece must have a maximum size.

+6
source share
2 answers

Zip4j supports creating a split zip file. Here's a sample for creating a split zip file (example from the Zip4j sample package )

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

is a method of creating a split zip file. boolean splitArchive should be set to true in this case. You can set the maximum file size for each split file (z01, z02, etc.) using 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(); } } 
+8
source

using this method and stream, we can pin the file

 private long totalFileSize = 0L; /* In bytes */ private long totalNumberFilesSelected = 0L; /* Total number of files selected */ private int partitionSize; /* In MegaBytes */ private int partitionSizeInBytes = 100000000; /* defaulted to 100 MB */ private int compressionLevel = 9; /* defaults to 9 (best) */ private String saveFileBase = ""; /* Zip file to save as */ private ArrayList fileInfoList; private ArrayList fileIndices = new ArrayList(); private int computeNumberOfArchives() private void createZipArchive() private void doZip() 

for a more detailed description of u, you can go to the link

http://www.codeproject.com/Articles/25453/Automating-Multipart-Zip-File-Creation

-4
source

Source: https://habr.com/ru/post/953046/


All Articles