2016-06-05 10 views
2

기존 .zip 파일을 수정 한 다음 수정 된 사본을 작성하려고합니다. 내가 쉽게 오류java.util.zip.ZipException 오류 : 1 zip 파일에서 다른 zip 파일로 이미지 (.png)를 복사하는 동안 유효하지 않은 항목 크기

java.util.zip.ZipException 결과 zip 파일의 .PNG 파일을 제외한 모든 파일에 그렇게 할 수

: 잘못된 항목 압축 된 크기 (113,177 예상하지만 있어요 113312 바이트)

다음 코드는 dice.zip에서 .png 이미지를 복사하고 diceUp.zip에 추가하기 위해 실행하려고하는 코드입니다.

public class Test { 
public static void main(String[] args) throws IOException{ 
    ZipFile zipFile = new ZipFile("dice.zip"); 
    final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("diceUp.zip")); 
    for(Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) { 
     ZipEntry entryIn = (ZipEntry) e.nextElement(); 
      if(entryIn.getName().contains(".png")){ 
       System.out.println(entryIn.getName()); 
       zos.putNextEntry(entryIn); 
       InputStream is = zipFile.getInputStream(entryIn); 
       byte [] buf = new byte[1024]; 
       int len; 
       while((len = (is.read(buf))) > 0) {    
        zos.write(buf, 0, (len < buf.length) ? len : buf.length); 
       } 
     } 
     zos.closeEntry(); 
    } 
    zos.close(); 
} 

답변

2

그냥 entryIn 객체의 이름으로 새로운 ZipEntry를 개체를 만들고 zos.putNextEntry이 새로운 객체를 넣어.
이 부분을보세요. qustion!

public static void main(String[] args) throws IOException { 
    ZipFile zipFile = new ZipFile("/home/*********/resources/dice.zip"); 
//  ZipFile zipFile = new ZipFile(); 
    final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("diceUp.zip")); 
    for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) { 
     ZipEntry entryIn = e.nextElement(); 
     if (entryIn.getName().contains(".png")) { 
      System.out.println(entryIn.getName()); 
      ZipEntry zipEntry = new ZipEntry(entryIn.getName()); 
      zos.putNextEntry(zipEntry); 
      InputStream is = zipFile.getInputStream(entryIn); 
      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = (is.read(buf))) > 0) { 
//     zos.write(buf, 0, (len < buf.length) ? len : buf.length); 
       zos.write(buf); 
      } 
     } 
     zos.closeEntry(); 
    } 
    zos.close(); 
} 
+0

당신에게 너무 감사합니다
는 그리고 이것은 내 코드입니다! 나는 지금 몇 시간 동안 고생하고있다. – Kira