Skip to content

Commit 8e2b749

Browse files
#493 Allow adding content to empty files
1 parent 025ce8c commit 8e2b749

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/main/java/net/lingala/zip4j/headers/HeaderReader.java

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public class HeaderReader {
6666

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

69+
if (zip4jRaf.length() == 0) {
70+
return new ZipModel();
71+
}
72+
6973
if (zip4jRaf.length() < ENDHDR) {
7074
throw new ZipException("Zip file size less than minimum expected zip file size. " +
7175
"Probably not a zip file or a corrupted zip file");

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

+16
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,22 @@ public void testAddStreamWithStoreCompressionAndAesEncryptionWorksFine() throws
949949
extractZipFileWithStream(generatedZipFile, PASSWORD);
950950
}
951951

952+
@Test
953+
public void testAddStreamToAnEmptyFileDoesNotThrowException() throws IOException {
954+
if (!generatedZipFile.createNewFile()) {
955+
throw new RuntimeException("Cannot create an empty file to test");
956+
}
957+
File fileToAdd = TestUtils.getTestFileFromResources("sample.pdf");
958+
try (ZipFile zipFile = new ZipFile(generatedZipFile);
959+
InputStream inputStream = Files.newInputStream(fileToAdd.toPath())) {
960+
ZipParameters zipParameters = new ZipParameters();
961+
zipParameters.setFileNameInZip(fileToAdd.getName());
962+
zipFile.addStream(inputStream, zipParameters);
963+
}
964+
965+
ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1);
966+
}
967+
952968
@Test
953969
public void testAddFolderWithCustomBufferSize() throws IOException {
954970
ZipFile zipFile = new ZipFile(generatedZipFile);

0 commit comments

Comments
 (0)