Skip to content

Commit

Permalink
#493 Allow adding content to empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Feb 20, 2023
1 parent 025ce8c commit 8e2b749
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/net/lingala/zip4j/headers/HeaderReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public class HeaderReader {

public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Zip4jConfig zip4jConfig) throws IOException {

if (zip4jRaf.length() == 0) {
return new ZipModel();
}

if (zip4jRaf.length() < ENDHDR) {
throw new ZipException("Zip file size less than minimum expected zip file size. " +
"Probably not a zip file or a corrupted zip file");
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/net/lingala/zip4j/AddFilesToZipIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,22 @@ public void testAddStreamWithStoreCompressionAndAesEncryptionWorksFine() throws
extractZipFileWithStream(generatedZipFile, PASSWORD);
}

@Test
public void testAddStreamToAnEmptyFileDoesNotThrowException() throws IOException {
if (!generatedZipFile.createNewFile()) {
throw new RuntimeException("Cannot create an empty file to test");
}
File fileToAdd = TestUtils.getTestFileFromResources("sample.pdf");
try (ZipFile zipFile = new ZipFile(generatedZipFile);
InputStream inputStream = Files.newInputStream(fileToAdd.toPath())) {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setFileNameInZip(fileToAdd.getName());
zipFile.addStream(inputStream, zipParameters);
}

ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1);
}

@Test
public void testAddFolderWithCustomBufferSize() throws IOException {
ZipFile zipFile = new ZipFile(generatedZipFile);
Expand Down

0 comments on commit 8e2b749

Please sign in to comment.