解压缩具有子文件夹的文件时的FileNotFound

时间:2015-03-03 22:51:44

标签: android unzip android-file

我有一个UnZip类来从数据库中获取图像

class UnZip extends AsyncTask<String, Integer, String> {
    private  String _mArchivePath;
    private  String _mOutPutStream;
    private int per;

    public UnZip(String mArchivePath,String mOutPutStream) {
        _mArchivePath = mArchivePath;
        _mOutPutStream = mOutPutStream;

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        InputStream inputstream; 
        ZipInputStream zipinputstream; 
        try { 
         String filename; 
         inputstream = new FileInputStream(_mArchivePath); 
         zipinputstream = new ZipInputStream(new BufferedInputStream(inputstream)); 
         ZipEntry mZipEntry; 
         byte[] buffer = new byte[32*1024]; 
         int count; 

         while ((mZipEntry = zipinputstream.getNextEntry()) != null) { 
          Log.v("Decompress", "Unzipping " + mZipEntry.getName());
          filename = mZipEntry.getName();
          per++;
          publishProgress(per);

          if (mZipEntry.isDirectory()) { 
           File fmd = new File(_mOutPutStream + filename); 
           fmd.mkdirs(); 
           continue; 
          } 

          FileOutputStream fileoutputstream = new FileOutputStream(_mOutPutStream + filename); 

          while ((count = zipinputstream.read(buffer)) != -1) { 
           fileoutputstream.write(buffer, 0, count); 
          } 


          fileoutputstream.close(); 
          zipinputstream.closeEntry(); 
         } 

         zipinputstream.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 

        } 
        return null;

它适用于包含图像的数据库(32MB~),但是当我尝试使用相同的database.zip时,但是有一些额外的图像(43MB~),它给了我这个错误:

03-03 23:42:00.200: V/Decompress(11593): Unzipping /database/weed/ak_47_1.jpg
03-03 23:42:00.202: W/System.err(11593): java.io.FileNotFoundException: /storage/emulated/0/unzipped/database/weed/ak_47_1.jpg: open failed: ENOENT (No such file or directory)
03-03 23:42:00.204: W/InputMethodManagerService(584): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@cce61f3 attribute=android.view.inputmethod.EditorInfo@aa9eeb0, token = android.os.BinderProxy@2db5667

我发现了问题,当我尝试用子文件夹解压缩文件时,如果子文件夹不存在,应用程序会给我这个错误。我几乎改变了主要代码,所以当应用程序启动它时会自动创建文件夹,因为我知道zip的结构,但是如果我想使用另一个zip我该怎么办?

1 个答案:

答案 0 :(得分:0)

看起来你用来解压缩ZIP的代码有问题,即子文件夹的创建并没有按预期工作。

最好的办法就是调查一下,正如@BrentM建议的那样,如果你匆忙,快速搜索SO可能会有所帮助:

How to unzip files programmatically in Android?

我在该线程中使用了以下方法,并且可以确认它的效果非常好:

private boolean unpackZip(String path, String zipname)
{       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         String filename;
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));          
         ZipEntry ze;
         byte[] buffer = new byte[1024];
         int count;

         while ((ze = zis.getNextEntry()) != null) 
         {
             filename = ze.getName();

             // Need to create directories if not exists, or
             // it will generate an Exception...
             if (ze.isDirectory()) {
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
             }

             FileOutputStream fout = new FileOutputStream(path + filename);

             while ((count = zis.read(buffer)) != -1) 
             {
                 fout.write(buffer, 0, count);             
             }

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

         zis.close();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}