|
8 | 8 | import net.lingala.zip4j.model.enums.CompressionLevel; |
9 | 9 | import net.lingala.zip4j.model.enums.CompressionMethod; |
10 | 10 | import net.lingala.zip4j.model.enums.EncryptionMethod; |
| 11 | +import net.lingala.zip4j.testutils.TestUtils; |
11 | 12 | import net.lingala.zip4j.testutils.ZipFileVerifier; |
12 | 13 | import net.lingala.zip4j.util.FileUtils; |
13 | 14 | import net.lingala.zip4j.util.InternalZipConstants; |
|
19 | 20 | import java.io.File; |
20 | 21 | import java.io.FilenameFilter; |
21 | 22 | import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
22 | 26 | import java.util.ArrayList; |
23 | 27 | import java.util.List; |
24 | 28 |
|
@@ -281,6 +285,66 @@ public void testCreateZipFileWithDeflateNoCompressionLevel() throws IOException |
281 | 285 | verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 3); |
282 | 286 | } |
283 | 287 |
|
| 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 | + |
284 | 348 | private void verifySplitZip(File zipFile, int numberOfExpectedSplitFiles, long splitLength) throws ZipException { |
285 | 349 | assertNumberOfSplitFile(zipFile, numberOfExpectedSplitFiles); |
286 | 350 | assertSplitFileSizes(zipFile, numberOfExpectedSplitFiles, splitLength); |
|
0 commit comments