2014-09-22 22 views
5

Benim oluşturulan zip dosyası ile ilgili bir sorunum var. Java 7 kullanıyorum. İki veya daha fazla Excel dosyası içeren bir bayt dizisinden bir zip dosyası oluşturmaya çalıştım. Uygulama, herhangi bir istisna olmaksızın her şeyi tamamlar. Yani, her şeyin yolunda olduğunu düşündüm. Zip dosyasını açmaya çalıştıktan sonra, Windows 7'den, zip dosyasının bozuk olabileceğinden bir hata mesajı oluştu. Açamadım ve neden bilmiyorum ... Bu sorun için yönettim, ancak bulduğum kod parçacıkları tam olarak uygulamamla aynı görünüyor.Bellekte zip dosyası oluşturma []. Zip dosyası her zaman bozuk

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) 
    { 
    ... 
    } 
} 

Bu çok basit bir başarısızlık olmalı ama bunu bulamıyor:

Bu

benim kodudur. Sorunumda bana yardım edersen hoş olur. Çok teşekkürler şimdiden. Eğer ZipOutputStream kapatmış önce

cevap

12

Sen ByteArrayOutputStreambyte[] bir dönüşmüyor.

try 
    { 
    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 blocks as before, finally is no longer required as the try-with-resources 
    // will ensure the streams are closed 
+1

Dostum, sen benim kahraman bugün için: Eğer baos.toByteArray() do önce zos sağlamalıdır kapalı olduğu, bunu sağlamanın en kolay yolu bir try-ile-kaynaklar yapıdır! :-) Sonunda işe yarıyor. :-)) – F4k3d

İlgili konular