I'm using the zip4j
library to zip and unzip files (xml
in particular). For an incoming String
, I need to write the contents of the String
into an xml
file, and then zip that file into a zip-file, before transferring the byte[]
content of the zip file into an apache camel
body object. The zipping part works alright, no issues here.
When I'm trying to read the message back into my application, I get the below error -
net.lingala.zip4j.exception.ZipException: unexpected end of file when reading short buff
at net.lingala.zip4j.core.HeaderReader.readIntoBuff(HeaderReader.java:1094)
at net.lingala.zip4j.core.HeaderReader.readCentralDirectory(HeaderReader.java:221)
at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:94)
at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:425)
at net.lingala.zip4j.core.ZipFile.getFileHeaders(ZipFile.java:688)
at ZipUtil.unzipFile(ZipUtil.java:230)
at ZipUtil.unzipFile(ZipUtil.java:218)
What I'm doing essentially is -
- Get the
byte[]
array from theexchange
object using the `exchange.getIn().getBody(byte[].class) method - Write the
byte[]
into a file - Since this is a zip payload, I rename the file to a
.zip
file - I then try to read the contents of the zip file into a
String
.
Here's the code -
public String extractMessageFromExchange(Exchange exchange) {
byte[] bytes = exchange.getIn().getBody(byte[].class);
File file = null;
File zipFile = null;
/*
* 1. Dump bytes into a file
* 2. Add ".zip" extension to file
* Now, Unzip the file (It's convoluted, but that's how we receive payload back from downstream)
* 3. Use ZipUtil to get path to the unzipped file
* 4. Use FileUtil to read the contents of the file into a String object
*/
try {
String filePath = zipFileGenPath + zipFileGenSuffix + hyphen +
incoming;
file = new File(filePath);
FileUtils.writeByteArrayToFile(file, bytes);
zipFile = new File(filePath+zipFileGenExt);
boolean renamed = file.renameTo(zipFile);
log.info("Temp zip file created at - " + zipFile.getPath());
String underlyingXMLFilePath =
unzipFile(zipFile.getPath(), zipFileGenPath); //gives the path
incomingMessage = FileUtils.readFileToString(new
File(underlyingXMLFilePath));
return incomingMessage;
} catch (Exception e) {
log.error("Exception occurred while reading byte[] payload into zip
file : ", e.getMessage());
log.error("Error stacktrace: ", e);
} finally {
deleteFile(zipFile);
}
}
And then the culprit code -
public String unzipFile(final String sourceZipFile, String updateToDir) throws Exception {
ZipFile zipFile = new ZipFile(sourceZipFile);
return unzipFile(zipFile, updateToDir);
}
public String unzipFile(ZipFile zipFile, String updateToDir) throws Exception {
ZipInputStream is = null;
OutputStream os = null;
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(false);
try {
List fileHeaderList = zipFile.getFileHeaders();
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
if (fileHeader != null) {
String outFilePath = updateToDir + File.separator + fileHeader.getFileName();
File outFile = new File(outFilePath);
is = zipFile.getInputStream(fileHeader);
os = new FileOutputStream(outFile);
int readLen = -1;
byte[] buff = new byte[4096];
while ((readLen = is.read(buff)) != -1) {
os.write(buff, 0, readLen);
}
return outFilePath;
}
}
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
return null;
}
The result ->
ZipUtil - Exception occurred while reading byte[] payload into zip file :
ZipUtil - Error stacktrace:
net.lingala.zip4j.exception.ZipException: unexpected end of file when reading short buff
at net.lingala.zip4j.core.HeaderReader.readIntoBuff(HeaderReader.java:1094)
at net.lingala.zip4j.core.HeaderReader.readCentralDirectory(HeaderReader.java:221)
at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:94)
at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:425)
at net.lingala.zip4j.core.ZipFile.getFileHeaders(ZipFile.java:688)
at ZipUtil.unzipFile(ZipUtil.java:230)
at ZipUtil.unzipFile(ZipUtil.java:218)
at ZipUtil.extractMessageFromExchange(ZipUtil.java:96)
Any idea what's happening? PS. I'm trying these in a Unix box.