Skip to content

Commit c4c993c

Browse files
#497 Create split zip file from stream
1 parent 62e7868 commit c4c993c

3 files changed

Lines changed: 107 additions & 3 deletions

File tree

src/main/java/net/lingala/zip4j/ZipFile.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public ZipFile(File zipFile, char[] password) {
164164
* @param parameters - zip parameters for this file list
165165
* @param splitArchive - if archive has to be split or not
166166
* @param splitLength - if archive has to be split, then length in bytes at which it has to be split
167-
* @throws ZipException
167+
* @throws ZipException - if zip file already exists, or of split length is less than 65536
168168
*/
169169
public void createSplitZipFile(List<File> filesToAdd, ZipParameters parameters, boolean splitArchive,
170170
long splitLength) throws ZipException {
@@ -180,12 +180,42 @@ public void createSplitZipFile(List<File> filesToAdd, ZipParameters parameters,
180180

181181
createNewZipModel();
182182
zipModel.setSplitArchive(splitArchive);
183-
zipModel.setSplitLength(splitLength);
183+
zipModel.setSplitLength(splitArchive ? splitLength : -1);
184184

185185
new AddFilesToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute(
186186
new AddFilesToZipTaskParameters(filesToAdd, parameters, buildConfig()));
187187
}
188188

189+
/**
190+
* Creates a split zip file from the input stream if splitArchive flag is set to true. If this flag is set to false
191+
* this method behaves as creating a regular (non-split) zip file. Split Length has to be more than 65536 bytes
192+
*
193+
* @param inputStream stream to add to the zip file
194+
* @param parameters zip parameters to consider when creating the zip file
195+
* @param splitArchive true if zip file has to be split, false otherwise
196+
* @param splitLength length in bytes at which the zip file has to be split
197+
* @throws ZipException if zip file already exists, or of split length is less than 65536
198+
*/
199+
public void createSplitZipFile(InputStream inputStream, ZipParameters parameters, boolean splitArchive,
200+
long splitLength) throws ZipException {
201+
202+
if (zipFile.exists()) {
203+
throw new ZipException("zip file: " + zipFile
204+
+ " already exists. To add files to existing zip file use addFile method");
205+
}
206+
207+
if (inputStream == null) {
208+
throw new ZipException("input stream is null, cannot create zip file");
209+
}
210+
211+
createNewZipModel();
212+
zipModel.setSplitArchive(splitArchive);
213+
zipModel.setSplitLength(splitArchive ? splitLength : -1);
214+
215+
new AddStreamToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute(
216+
new AddStreamToZipTaskParameters(inputStream, parameters, buildConfig()));
217+
}
218+
189219
/**
190220
* Creates a zip file and adds the files/folders from the specified folder to the zip file.
191221
* This method does the same functionality as in addFolder method except that this method

src/test/java/net/lingala/zip4j/CreateZipFileIT.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.lingala.zip4j.model.enums.CompressionLevel;
99
import net.lingala.zip4j.model.enums.CompressionMethod;
1010
import net.lingala.zip4j.model.enums.EncryptionMethod;
11+
import net.lingala.zip4j.testutils.TestUtils;
1112
import net.lingala.zip4j.testutils.ZipFileVerifier;
1213
import net.lingala.zip4j.util.FileUtils;
1314
import net.lingala.zip4j.util.InternalZipConstants;
@@ -19,6 +20,9 @@
1920
import java.io.File;
2021
import java.io.FilenameFilter;
2122
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
2226
import java.util.ArrayList;
2327
import java.util.List;
2428

@@ -281,6 +285,66 @@ public void testCreateZipFileWithDeflateNoCompressionLevel() throws IOException
281285
verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 3);
282286
}
283287

288+
@Test
289+
public void testCreateZipFileFromStreamThrowsExceptionIfZipFileExists() throws IOException {
290+
try (ZipFile zipFile = new ZipFile(generatedZipFile)) {
291+
zipFile.addFile(TestUtils.getTestFileFromResources("sample.pdf"));
292+
}
293+
294+
expectedException.expect(ZipException.class);
295+
expectedException.expectMessage("zip file: " + generatedZipFile
296+
+ " already exists. To add files to existing zip file use addFile method");
297+
298+
try (ZipFile zipFile = new ZipFile(generatedZipFile)) {
299+
zipFile.createSplitZipFile((InputStream) null, new ZipParameters(), true, 512000);
300+
}
301+
}
302+
303+
@Test
304+
public void testCreateZipFileFromStreamCreatesSplitFileSuccessfully() throws IOException {
305+
Path fileToAdd = TestUtils.getTestFileFromResources("file_PDF_1MB.pdf").toPath();
306+
ZipParameters zipParameters = new ZipParameters();
307+
zipParameters.setFileNameInZip(fileToAdd.getFileName().toString());
308+
309+
try (ZipFile zipFile = new ZipFile(generatedZipFile);
310+
InputStream inputStream = Files.newInputStream(fileToAdd)) {
311+
zipFile.createSplitZipFile(inputStream, zipParameters, true, 512000);
312+
}
313+
314+
verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1);
315+
verifySplitZip(generatedZipFile, 2, 512000);
316+
}
317+
318+
@Test
319+
public void testCreateZipFileFromStreamCreatesNonSplitFileWhenFlagIsFalse() throws IOException {
320+
Path fileToAdd = TestUtils.getTestFileFromResources("file_PDF_1MB.pdf").toPath();
321+
ZipParameters zipParameters = new ZipParameters();
322+
zipParameters.setFileNameInZip(fileToAdd.getFileName().toString());
323+
324+
try (ZipFile zipFile = new ZipFile(generatedZipFile);
325+
InputStream inputStream = Files.newInputStream(fileToAdd)) {
326+
zipFile.createSplitZipFile(inputStream, zipParameters, false, 512000);
327+
}
328+
329+
verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1);
330+
verifySplitZip(generatedZipFile, 1, 512000);
331+
}
332+
333+
@Test
334+
public void testCreateZipFileFromStreamCreatesNonSplitFileWhenSplitLengthIsLowerThanFileLength() throws IOException {
335+
Path fileToAdd = TestUtils.getTestFileFromResources("sample.pdf").toPath();
336+
ZipParameters zipParameters = new ZipParameters();
337+
zipParameters.setFileNameInZip(fileToAdd.getFileName().toString());
338+
339+
try (ZipFile zipFile = new ZipFile(generatedZipFile);
340+
InputStream inputStream = Files.newInputStream(fileToAdd)) {
341+
zipFile.createSplitZipFile(inputStream, zipParameters, true, 512000);
342+
}
343+
344+
verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1);
345+
verifySplitZip(generatedZipFile, 1, 512000);
346+
}
347+
284348
private void verifySplitZip(File zipFile, int numberOfExpectedSplitFiles, long splitLength) throws ZipException {
285349
assertNumberOfSplitFile(zipFile, numberOfExpectedSplitFiles);
286350
assertSplitFileSizes(zipFile, numberOfExpectedSplitFiles, splitLength);

src/test/java/net/lingala/zip4j/ZipFileTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import java.io.ByteArrayInputStream;
1313
import java.io.File;
1414
import java.io.IOException;
15+
import java.io.InputStream;
1516
import java.util.Collections;
17+
import java.util.List;
1618

1719
import static net.lingala.zip4j.util.InternalZipConstants.MIN_BUFF_SIZE;
1820
import static org.assertj.core.api.Assertions.assertThat;
@@ -68,7 +70,7 @@ public void testCreateZipFileThrowsExceptionWhenFileListIsNull() throws ZipExcep
6870
expectedException.expect(ZipException.class);
6971
expectedException.expectMessage("input file List is null, cannot create zip file");
7072

71-
zipFile.createSplitZipFile(null, new ZipParameters(), true, 10000);
73+
zipFile.createSplitZipFile((List<File>) null, new ZipParameters(), true, 10000);
7274
}
7375

7476
@Test
@@ -79,6 +81,14 @@ public void testCreateZipFileThrowsExceptionWhenFileListIsEmpty() throws ZipExce
7981
zipFile.createSplitZipFile(Collections.<File>emptyList(), new ZipParameters(), true, 10000);
8082
}
8183

84+
@Test
85+
public void testCreateZipFileFromStreamThrowsExceptionIfStreamIsNull() throws IOException {
86+
expectedException.expect(ZipException.class);
87+
expectedException.expectMessage("input stream is null, cannot create zip file");
88+
89+
zipFile.createSplitZipFile((InputStream) null, new ZipParameters(), true, 512000);
90+
}
91+
8292
@Test
8393
public void testCreateZipFileFromFolderThrowsExceptionWheFolderIsNull() throws ZipException {
8494
expectedException.expect(ZipException.class);

0 commit comments

Comments
 (0)