2014-09-22 21 views
5

Tôi gặp sự cố với tệp zip đã tạo của mình. Tôi đang sử dụng Java 7. Tôi đã cố tạo một tệp zip trong một mảng byte, chứa hai hoặc nhiều tệp Excel. Ứng dụng hoàn thành allways mà không có bất kỳ trường hợp ngoại lệ. Vì vậy, tôi nghĩ mọi thứ đều ổn. Sau khi tôi cố mở tệp nén, có một thông báo lỗi từ Windows 7, tệp zip có thể bị hỏng. Tôi không thể mở nó và tôi không biết tại sao ...! Tôi googled cho vấn đề này nhưng đoạn mã tôi tìm thấy, trông chính xác giống như trong thực hiện của tôi.Tạo tệp zip trong bộ nhớ ra khỏi byte []. Tệp zip là allways bị hỏng

Đây là mã của tôi:

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

Nó phải là một thất bại rất đơn giản nhưng tôi không thể tìm thấy nó. Sẽ rất tuyệt nếu bạn có thể giúp tôi giải quyết vấn đề của mình. Cảm ơn rất nhiều trước.

Trả lời

12

Bạn đang chuyển đổi ByteArrayOutputStream thành byte[] trước khi bạn đóng ZipOutputStream. Bạn phải đảm bảo zos được đóng trước khi bạn làm baos.toByteArray(), cách dễ nhất để đảm bảo đây là một cấu trúc thử-với-nguồn:

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

Dude, bạn là anh hùng của tôi cho ngày hôm nay! :-) Nó hoạt động, cuối cùng. :-)) – F4k3d

Các vấn đề liên quan