gpt4 book ai didi

java - 将 Zip 内容读入字符串时出现 "ZipException - unexpected end of file when reading short buff"

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:56 31 4
gpt4 key购买 nike

我正在使用 zip4j 库来压缩和解压缩文件(特别是 xml)。对于传入的 String,我需要将 String 的内容写入 xml 文件,然后将该文件压缩为 zip 文件,然后将 zip 文件的 byte[] 内容传输到 apache Camel body 对象。压缩部分工作正常,没有问题。

当我尝试将消息读回我的应用程序时,出现以下错误 -

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)

我所做的本质上是 -

  1. 使用 `exchange.getIn().getBody(byte[].class) 方法从 exchange 对象获取 byte[] 数组
  2. byte[]写入文件
  3. 由于这是 zip 有效负载,因此我将文件重命名为 .zip 文件
  4. 然后,我尝试将 zip 文件的内容读入 String

这是代码 -

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);
}
}

然后是罪魁祸首代码 -

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;
}

结果->

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)

知道发生了什么吗?附言。我正在 Unix 盒子里尝试这些。

最佳答案

我成功解决了这个问题。问题不在于压缩逻辑或 zip4j,而在于解压开始之前的一个顽皮的小“convertBodyToString”方法。这实际上导致了原始压缩消息的损坏,因此解压无法成功。

据您所知,文本末尾的一个 ~ 字符破坏了它。

关于java - 将 Zip 内容读入字符串时出现 "ZipException - unexpected end of file when reading short buff",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567370/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com