在安卓系统中解压大文件

3

我有一个zip文件,里面包含两个文件,一个是md5sum,另一个是3.5GB的.img文件。我的应用程序以zip文件的形式下载它们,然后需要在设备上解压缩。 目前我正在使用下面的内部类,已经测试过可以处理更小的zip文件:

private class UnZip extends AsyncTask<Void, Integer, Integer> {

        private String _zipFile;   
        private String _location;
        private int per = 0;

        public UnZip(String zipFile, String location) {
            _zipFile = zipFile;     
            _location = location;      
            _dirChecker("");   
        }    

        protected Integer doInBackground(Void... params) {
            try  {       
                ZipFile zip = new ZipFile(_zipFile);
                bar.setMax(zip.size());
                FileInputStream fin = new FileInputStream(_zipFile);       
                ZipInputStream zin = new ZipInputStream(fin);
                ZipEntry ze = null;       
                while ((ze = zin.getNextEntry()) != null) {

                    Log.v("Decompress", "Unzipping " + ze.getName());          
                    if(ze.isDirectory()) {           
                        _dirChecker(ze.getName());         
                    } else {      
                        // Here I am doing the update of my progress bar
                        Log.v("Decompress", "more " + ze.getName());          

                        per++;
                        publishProgress(per);

                        FileOutputStream fout = new FileOutputStream(_location +ze.getName());           
                        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);    
            }    
            return null;
        }    

        protected void onProgressUpdate(Integer... progress) {
            bar.setProgress(per); //Since it's an inner class, Bar should be able to be called directly
        }

        protected void onPostExecute(Integer... result) {
            Log.i("UnZip" ,"Completed. Total size: "+result);
        }

        private void _dirChecker(String dir) {     
            File f = new File(_location + dir);      
            if(!f.isDirectory()) {       
                f.mkdirs();     
            }   
        }

    }

这种方法可以很好地显示进度条,每个文件解压缩时都会显示进度条,但对于大文件(在我的Nexus 4上每小时约20MB)需要非常长的时间。 我想知道是否有更好的方法来解压缩这样一个大文件? (.img文件实际上只有约1GB的数据,其余都是末尾的零以便留出更多数据空间)
或者也许有一种方法可以按每个MB数据或写入速度等方式查看进展情况?从长远来看,向用户提供有关解压缩进度的更多信息将非常有用。

for (int c = zin.read(); c != -1; c = zin.read()) 逐字节读取,读入一个大小约为4-32k的 byte[] 缓冲区,最后一次性将整个缓冲区写入。 - zapl
你能提供一些示例代码吗,@zapl?我想这会加快数据提取的速度。看起来它正在工作,但到目前为止,3个小时只完成了60MB :/ - DevWithZachary
1个回答

3
请将以下内容添加至任意位置:

public static void streamCopy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[32 * 1024]; // play with sizes..
    int readCount;
    while ((readCount = in.read(buffer)) != -1) {
        out.write(buffer, 0, readCount);
    }
}

这是一种普遍有用的标准代码,可以将输入流复制到输出流中,并在多个库中找到。

然后在您的代码中使用它。

} else {
    // Here I am doing the update of my progress bar
    Log.v("Decompress", "more " + ze.getName());

    per++;
    publishProgress(per);

    FileOutputStream fout = new FileOutputStream(_location + ze.getName());

    streamCopy(zin, fout);

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

优点是你可以读写更大的块而不是单个字节,这样可以大大加快进程。


1
太好了!现在解压缩不到10分钟了!! - DevWithZachary
@ZacPowell 在不到10分钟内处理3.5GB的数据意味着你可以达到大约6MB/s的吞吐量,听起来接近安卓设备的性能。内部存储的最大写入速度大约为10-20MB/s,但由于在写入数据之前需要读取和解压缩,所以无法达到这个速度。 - zapl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接