2

我在 Android 中解压缩文件时遇到问题。这是代码片段:

 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)像空文件一样解压缩 - 大小为 0(html 文件,.jpg)。其他文件没问题。如果我使用相同的代码,但没有缓冲区,所有文件都可以 - 当然,没有缓冲区的解压缩是不可能的,因为它运行时间太长。文件存储在真实设备上的 SD 卡上。我已经尝试过设置更小的缓冲区大小(甚至是新字节 [2])。提前致谢...

4

1 回答 1

3

试试这个代码,

    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"))
        );
    }

}
于 2012-08-29T13:29:19.100 回答