问题描述
我的Java zip问题仅在linux环境中的websphere中出现, 但是可以在我本地的Windows开发人员计算机上正常使用。
我尝试压缩内容并将结果作为邮件发送,并将相同的内容备份在文件夹中。
我将输入字节数组压缩为zip结果字节数组。
我尝试将压缩结果数据保存在一个文件夹中,并发送与邮件附件相同的内容。
结果:邮件中的zip附件已正确传递,但保存为文件的同一zip字节数组已损坏。
public static byte[] zipContent(String filename,byte[] content) throws IOException {
final CRC32 crc = new CRC32();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (final ZipOutputStream zipOut = new ZipOutputStream(baos);
final InputStream fis = new ByteArrayInputStream(content);) {
crc.reset();
final ZipEntry zipEntry = new ZipEntry(filename);
zipOut.putNextEntry(zipEntry);
int length;
byte[] bytes = new byte[1024];
while ((length = fis.read(bytes)) >= 0) {
crc.update(bytes,length);
zipOut.write(bytes,length);
}
final long crcValue = crc.getValue();
zipEntry.setCrc(crcValue);
zipEntry.setSize(content.length);
zipEntry.setComment("CRC_" + Long.toString(crcValue));
zipOut.closeEntry();
zipOut.flush();
} finally {
baos.flush();
baos.close();
}
return baos.toByteArray();
}
我打电话
final byte[] csvContentArray = ... // Get CSV input anyway ...
File csvFile = new File(aCsvPathString);
Files.write(csvfile.toPath(),csvContentArray); // OKAY,saved correctly pre compress in server
//COMPRESS: byte[] --> byte[]
final byte[] zippedContent = zipContent(csvFileName,csvContentArray);
File zipFile = new File(aZipPathString);
Files.write(zipfile.toPath(),zippedContent); // CORRUPTED,saved after compress in server !!!
...
sendMail(emailAdress,zippedContent); // the same zipped content in mail is delivered correctly and can be opened without warnings !
将zip文件内容作为字节[]保存在文件系统中与将其作为邮件附件(MimeBodyPart)发送之间有所不同。
这对我来说很奇怪,因为它在Windows / Tomee中可以正常工作,但在Linux / Websphere中却不能。
我的压缩后的csv文件很大,压缩后,大约1000行就可以了。其余文件混乱。
我尝试使用ZipEntry对带有和不带有CRC的代码进行编码,但这没关系。
在7zip中打开损坏的zip会给出如下警告:
未修改日期日期。翻译:数据意外结束。
Es gibt noch Daten hinter den Hauptdaten。已翻译:主数据之后的现有数据。
我总是很感激大家的意见和建议。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)