I’ve a simple Java code which creates a ZIP file using one CSV file. The code is working fine and produces the zip file just right. But, the zip file size(bytes) are different that the one I create using Windows zipping tool or something like 7zip. I need to know if there is any Java library which can create zip file similar to how windows zips the file.
Background – We send this zip file to a REST API which fails sometimes with 403 – Forbidden error but when we zip the file using windows zipper or 7zip, it works fine. So, I would like to know if there is any way to zip file in Java the way windows/7zip does.
I’ve tried –
- Inbuilt Java functions for zipping
- Apache commons compress
- zip4j
e.g.
Map<String, String> env = new HashMap<>(); // Create the zip file if it doesn't exist env.put("create", "true"); URI uri = URI.create("jar:file:/C:/temp/test.zip"); try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) { Path externalTxtFile = Paths.get("C:/temp/test.csv"); Path pathInZipfile = zipfs.getPath("/test.csv"); // Copy a file into the zip file Files.copy(externalTxtFile, pathInZipfile, StandardCopyOption.REPLACE_EXISTING); }
PS. We are still waiting for stack trace details from the API provider but in the meantime, I am looking for something which can generate exact replica of zip file generated by OS e.g. Windows Zipper.
Update I tried setting compression level to 1, 2,3,4,5 it works with all of these. I tried setting 7,8,9 it works again. But with compression level 6, its failing. Any idea what could be the reason? My code runs on Unix OS so I believe 6 is the default level. But no idea how compression ratio might affect here.
FileOutputStream fout = new FileOutputStream("C:\temp\productcost.zip"); ZipOutputStream zout = new ZipOutputStream(output); zout.setLevel(5); //1,2,3,4,5,7,8,9 works Level 6 - Fails with 403 Forbidden Path file = Paths.get("C:\temp\productcost.csv"); byte[] bytes = Files.readAllBytes(file); ZipEntry ze = new ZipEntry("productcost.csv"); zout.putNextEntry(ze); zout.write(bytes, 0, bytes.length); zout.closeEntry(); zout.finish(); zout.flush(); zout.close();
Also, if I remove first character from the CSV file or add a character manually at the beginning, the file works fine without any level setting. I don’t see any BOM characters in the file.
Advertisement
Answer
There was an bug/issue with the endpoint API which was failing to decompress some of the zipped files with default compression level of 6. The same files were processing through with any other compression level. Since we don’t own it, we don’t know what exactly did they fixed. But, everything is working fine now. Will update this answer if I hear back from the API providers.