mrossi
March 12, 2025, 3:33pm
1
I am using a simple code:
try (IArchive archive = getArchive()) {
archive.extractToDirectory(outputDir.toString());
} finally {
zipInputStream.close();
}
I am trying to unzip a ~230MB file, and with the “-Xmx512m” option (max 512 MB of heap) I get the out of heap exception.
This means that unzipping a 230MB zip file takes more than double the size of it, which is unwanted.
This happens only if I pass to the archive a ByteArrayInputStream with the bytes of the zip file.
mrossi
March 12, 2025, 3:40pm
3
This is an example of class/method that can fit the 512MB heap
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
public class FileUnzipper {
private FileUnzipper() {
}
private static final int BUFFER_SIZE = 8 << 10;
public static void unzip(String zipPathname, String pathnameEncoding, String dirPathname) throws IOException {
try (FileInputStream in = new FileInputStream(zipPathname)) {
unzip(in, pathnameEncoding, dirPathname);
}
}
public static void unzip(byte[] zipBytes, String pathnameEncoding, String dirPathname) throws IOException {
try (ByteArrayInputStream in = new ByteArrayInputStream(zipBytes)) {
unzip(in, pathnameEncoding, dirPathname);
}
}
private static void unzip(InputStream in, String pathnameEncoding, String dirPathname) throws IOException {
byte[] b = new byte[BUFFER_SIZE];
if (!dirPathname.endsWith(File.separator))
dirPathname += File.separator;
try (ZipArchiveInputStream zin = new ZipArchiveInputStream(in, pathnameEncoding, false)) {
ZipArchiveEntry entry;
while ((entry = zin.getNextZipEntry()) != null) {
File file = new File(dirPathname + entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
file.getParentFile().mkdirs();
file.createNewFile();
try (FileOutputStream fout = new FileOutputStream(file)) {
int count;
while ((count = zin.read(b)) >= 0)
fout.write(b, 0, count);
}
file.setLastModified(entry.getTime());
}
}
}
}
}
Please provide the archive details (7z.exe l -slt archive.zip
) reported by 7-zip tool.
Does this error occur if you open the archive by path?
mrossi
March 13, 2025, 9:23am
5
If I open the archive by path, the RAM issue does not occur.
I am sending you a zip file in which the problem appears, it is too big to be attached here so I am sending a link that will last for some days.
@mrossi
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies .
Issue ID(s): ZIPJAVA-220
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
mrossi
March 17, 2025, 8:23am
7
Is it possible to know, approximately, in which version will the fix be applied?
The fix will be applied in version 25.3.
The issues you have found earlier (filed as ZIPJAVA-220) have been fixed in this update .
Download link: Aspose.ZIP for Java