5
我的創建zip文件有問題。我正在使用Java 7.我試圖從包含兩個或多個Excel文件的字節數組中創建一個zip文件。該應用程序完全沒有任何例外。所以,我認爲一切都很好。在我試圖打開zip文件後,Windows 7出現了一條錯誤消息,說明該zip文件可能已損壞。我無法打開它,我不知道爲什麼......! 我搜索了這個問題,但我發現的代碼片段,看起來和我的實現完全一樣。在字節[]中創建內存中的zip文件。 Zip文件全部損壞
這是我的代碼:
if (repsList.size() > 1)
{
String today = DateUtilities.convertDateToString(new Date(), "dd_MM_yyyy");
String prefix = "recs_" + today;
String suffix = ".zip";
ByteArrayOutputStream baos = null;
ZipOutputStream zos = null;
try
{
baos = new ByteArrayOutputStream();
zos = new ZipOutputStream(baos);
for (RepBean rep : repsList)
{
String filename = rep.getFilename();
ZipEntry entry = new ZipEntry(filename);
entry.setSize(rep.getContent().length);
zos.putNextEntry(entry);
zos.write(rep.getContent());
zos.closeEntry();
}
// this is the zip file as byte[]
reportContent = baos.toByteArray();
}
catch (UnsupportedEncodingException e)
{
...
}
catch (ZipException e) {
...
}
catch (IOException e)
{
...
}
finally
{
try
{
if (zos != null)
{
zos.close();
}
if (baos != null)
{
baos.close();
}
}
catch (IOException e)
{
// Nothing to do ...
e.printStackTrace();
}
}
}
try
{
response.setContentLength(reportContent.length);
response.getOutputStream().write(reportContent);
}
catch (IOException e)
{
...
}
finally
{
try
{
response.getOutputStream().flush();
response.getOutputStream().close();
}
catch (IOException e)
{
...
}
}
它必須是一個非常簡單的故障,但我不能找到它。如果你能幫我解決我的問題,那會很好。 非常感謝。
老兄,你是我的英雄今天! :-) 它最終奏效。 :-)) – F4k3d 2014-09-22 13:39:42