Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I decompress a large data file in AsyncTask.doInBackground?

Tags:

android

I have a large data file, which is zipped, and approximately 20MB. When it's unzipped, it's up to about 50MB. The following source code works fine. I found the original on the web somewhere else and modified it a bit. And this method is called within the AsyncTask.doInBackground.

So, what I want to know is, how can I save the on going status(? sorry, I don't know the proper English word) and resume the procedure later? I mean, this method takes a bit long time (about a minute on an emulator), and I know there is no way since the data is kind of huge. So, if a main activity of this method gets killed, I want to save the current status of decompressing the file, and when the activity gets active, I want to resume decompressing from the last point. Hope my explanation clears my intent.

I was thinking using a service, but I also want to interact with UI, such as showing a progress or whatever. I can't find good information to do that in the service when I roughly scan the reference, but is there a way to do that in the service? And do you think I should use it?

Anyway, my main point is how to resume decompressing a file.

private final static int CHUNK_SIZE = 32 * 1024;
byte[] _fileIOBuffer = new byte[CHUNK_SIZE];

public void unzipFile(DBFileDownloader downloader, File zipFile, String directory) 
    throws IOException
{ 
    ZipInputStream in = null; 
    FileOutputStream os = null; 
    try 
    {
        in = new ZipInputStream (new FileInputStream(zipFile)); 
        ZipEntry entry = null; 
        while ((entry = in.getNextEntry ())!= null) 
        { 
            String entryName = entry.getName();                 
            if (entry.isDirectory ()) { 
                File file = new File (directory, entryName); 
                file.mkdirs(); 
            } 
            else { 
                File file = new File(directory, entryName);
                if (file.exists()){
                    file.delete();  // I don't know how to append, so delete it always
                }
                os = new FileOutputStream (file); 

                int bytesRead = 0; 
                while ((bytesRead = in.read (_fileIOBuffer))!= -1) {
                    os.write(_fileIOBuffer, 0, bytesRead);
                    // progress procedure
                }
                os.close();
            }
        }
    }
    catch (FileNotFoundException e) {        
        Log.v("unzip", e.getMessage());
    } 
    catch (IOException e) {
        Log.v("unzip", e.getMessage());
    } 
    finally{
        if (in != null ){
            in.close();
        }
        if (os != null ){
            os.close();
        }
    }
}       

Thanks in advance, yokyo

like image 223
Yoo Matsuo Avatar asked Nov 05 '22 11:11

Yoo Matsuo


1 Answers

So, if a main activity of this method gets killed, I want to save the current status of decompressing the file, and when the activity gets active, I want to resume decompressing from the last point.

That will be extremely difficult, if not impossible.

I was thinking using a service, but I also want to interact with UI, such as showing a progress or whatever.

This is a fine plan. Just have the activity register a listener with the service, and the service calls that listener for "a progress or whatever".

like image 109
CommonsWare Avatar answered Nov 15 '22 08:11

CommonsWare