2012-08-29 4 views
2

Android에서 파일을 압축 해제하는 데 문제가 있습니다. - 크기 0 (HTML 파일 .jpg)이Android - 일부 압축 해제 된 파일의 크기가 0입니다 (비어 있음)

public void unzip() { 
try { 

    FileInputStream fin = new FileInputStream(_zipFile); 
    BufferedInputStream in = new BufferedInputStream(fin); 
    ZipInputStream zin = new ZipInputStream(in); 

    ZipEntry ze = null; 
    while ((ze = zin.getNextEntry()) != null) { 
    Log.v("Decompress", "Unzipping " + ze.getName()); 

    if(ze.isDirectory()) { 
     _dirChecker(ze.getName()); 
    } else { 
     FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
     BufferedOutputStream out = new BufferedOutputStream(fout); 

     byte[] buffer = new byte[1024]; 
     int length; 

     while ((length = zin.read(buffer,0,1024)) >= 0) { 
      out.write(buffer,0,length); 
     } 

     /* while ((length = zin.read(buffer))>0) { 
      out.write(buffer, 0, length); 
      }*/ 
     /*for (int c = zin.read(); c != -1; c = zin.read()) { 
     fout.write(c); 
     }*/ 

     zin.closeEntry(); 
     fout.close(); 
    } 

    } 
    zin.close(); 
} catch(Exception e) { 
    Log.e("Decompress", "unzip", e); 
} 
} 

작은 파일 (10kB에서보다 작은) 빈처럼 압축을 풀면됩니다 여기에 코드입니다. 다른 파일은 괜찮습니다. 이 동일한 코드를 사용하지만 버퍼가없는 경우 모든 파일이 ok입니다. 버퍼없이 압축을 풀면 너무 오래 실행되므로 문제가되지 않습니다. 파일은 실제 장치의 SD 카드에 저장됩니다. 나는 이미 작은 버퍼 크기 (심지어 새로운 바이트 [2])를 설정하려고 시도했다. 미리 감사드립니다 ...

답변

2

대신에이 코드를 시도

이 이
public void doUnzip(String inputZipFile, String destinationDirectory) 
     throws IOException { 
    int BUFFER = 2048; 
    List zipFiles = new ArrayList(); 
    File sourceZipFile = new File(inputZip); 
    File unzipDestinationDirectory = new File(destinationDirectory); 
    unzipDestinationDirectory.mkdir(); 

    ZipFile zipFile; 
    // Open Zip file for reading 
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); 

    // Create an enumeration of the entries in the zip file 
    Enumeration zipFileEntries = zipFile.entries(); 

    // Process each entry 
    while (zipFileEntries.hasMoreElements()) { 
     // grab a zip file entry 
     ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); 

     String currentEntry = entry.getName(); 

     File destFile = new File(unzipDestinationDirectory, currentEntry); 
//   destFile = new File(unzipDestinationDirectory, destFile.getName()); 

     if (currentEntry.endsWith(".zip")) { 
      zipFiles.add(destFile.getAbsolutePath()); 
     } 

     // grab file's parent directory structure 
     File destinationParent = destFile.getParentFile(); 

     // create the parent directory structure if needed 
     destinationParent.mkdirs(); 

     try { 
      // extract file if not a directory 
      if (!entry.isDirectory()) { 
       BufferedInputStream is = 
         new BufferedInputStream(zipFile.getInputStream(entry)); 
       int currentByte; 
       // establish buffer for writing file 
       byte data[] = new byte[BUFFER]; 

       // write the current file to disk 
       FileOutputStream fos = new FileOutputStream(destFile); 
       BufferedOutputStream dest = 
         new BufferedOutputStream(fos, BUFFER); 

       // read and write until last byte is encountered 
       while ((currentByte = is.read(data, 0, BUFFER)) != -1) { 
        dest.write(data, 0, currentByte); 
       } 
       dest.flush(); 
       dest.close(); 
       is.close(); 
      } 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
    zipFile.close(); 

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) { 
     String zipName = (String)iter.next(); 
     doUnzip(
      zipName, 
      destinationDirectory + 
       File.separatorChar + 
       zipName.substring(0,zipName.lastIndexOf(".zip")) 
     ); 
    } 

} 
+0

덕분에, 문제가 해결! – vlatko

+0

제 친구를 환영합니다 .. –

관련 문제