Android的 - 一些解压缩文件有大小为0(空) [英] Android - some unzipped files have 0 size (are empty)

查看:592
本文介绍了Android的 - 一些解压缩文件有大小为0(空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的一个问题,在Android的解压文件。这里是code片断:

i'm facing a problem with unzipping files in Android. Here is the code snippet:

 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格式)。其他文件都OK。如果我使用同样的code,但没有缓冲区中的所有文件都OK - ofcourse,无需解压的缓冲区是出了问题,因为它运行太长。文件存储在真实设备的SD卡上。我已经尝试设置较小的缓冲区大小(甚至是新的字节[2])。在此先感谢...

Smaller files (smaller than 10kB) are unzipped like empty - size 0 (html files, .jpg). Other files are ok. If I use this same code, but without buffers all the files are ok - ofcourse, unzipping without buffers is out of the question since it runs too long. Files are stored on SD card on real device. I have already tried setting smaller buffer size ( even new byte[2]). Thanks in advance...

推荐答案

试试这个code代替,

Try this code instead,

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

}

这篇关于Android的 - 一些解压缩文件有大小为0(空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆