2

I have an UnZip class to get images from a database

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;

It works great with a database.zip containing images(32MB~) but when I tried it with the same database.zip but with some images extra(43MB~) it gives me this error:

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

I found the problem, it comes when I try to unzip a file with subfolders, if the subfolders don't exist the app gives me this error. Momentarily I changed the main code, so when the app starts it automatically creates the folders, because I know the structure of the zip, but if I want to use another zip what can I do?

Leonardo
  • 499
  • 1
  • 7
  • 18
  • Check destination available storage is sufficient, then check the archive is valid by using another tool to extract the contents. Then take a look at the file that can't be uncompressed and just add that one file to new zip and try that. Try and track the problem down to that one file then step through the code to debug the issue. – BrentM Mar 04 '15 at 01:17
  • I found the problem, it comes when I try to unzip a file with subfolders, if the subfolders don't exist the app gives me this error. Momentarily I changed the main code, so when the app starts it automatically creates the folders, because I know the structure of the zip, but if I want to use another zip what can I do? – Leonardo Mar 04 '15 at 22:55
  • The mkdirs() may be failing for you. Step through the code to see why. Try changing the the constructor for the file from `new File(_mOutPutStream + filename)` to `new File(_mOutPutStream, filename)`. As the existing code will only work if you have a trailing slash on the folder name. – BrentM Mar 04 '15 at 23:12
  • I tried using the comma but it still not works – Leonardo Mar 05 '15 at 14:22

1 Answers1

1

It looks like there's something wrong with the code you use to unpack the ZIP, i.e. the creation of subfolders doesn't work as expected.

The best thing to do is to investigate that, as @BrentM suggested, however if you're in a rush, a quick search on SO could be of help:

How to unzip files programmatically in Android?

I used the following method from that thread and can confirm it works perfectly well:

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;
}
Dexter
  • 2,462
  • 4
  • 24
  • 28
JakeP
  • 1,736
  • 4
  • 23
  • 31
  • I tried also this code, but still give me error. Maybe I'm typing wrong the path and file. I use path= Evinronment.getExternalStorageDirectory().getPath() + "/"; file = "database.zip" – Leonardo Mar 05 '15 at 14:35
  • Are you sure the file is there? You can always look for it in a file manager to establish its real location. – JakeP Mar 05 '15 at 15:34
  • Well, you can recursively go through all the directories starting at getExternalStorageDirectory() and print all the files in there to see, where exactly your file is. – JakeP Mar 05 '15 at 20:57