如何加快 Java / Android 中的解压时间?

2024-04-06

在 Android 上解压缩文件似乎非常慢。起初我以为这只是模拟器,但在手机上似乎是一样的。我尝试了不同的压缩级别,最终降到存储模式,但仍然需要很长时间。

无论如何,总得有个理由吧!还有其他人有这个问题吗?我的解压方法如下所示:

public void unzip()
{
try{
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        File rootfolder = new File(directory);
        rootfolder.mkdirs();
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry())!=null){

            if(ze.isDirectory()){
                dirChecker(ze.getName());
            }
            else{
                FileOutputStream fout = new FileOutputStream(directory+ze.getName());

            for(int c = zin.read();c!=-1;c=zin.read()){
                fout.write(c);
            }
                //Debug.out("Closing streams");
                zin.closeEntry();
                fout.close();

        }
    }
    zin.close();
}
catch(Exception e){
            //Debug.out("Error trying to unzip file " + zipFile);

}
    }

我不知道在 Android 上解压缩是否很慢,但是在循环中逐字节复制肯定会减慢速度。尝试使用 BufferedInputStream 和 BufferedOutputStream - 它可能有点复杂,但根据我的经验,它最终是值得的。

BufferedInputStream in = new BufferedInputStream(zin);
BufferedOutputStream out = new BufferedOutputStream(fout);

然后你可以这样写:

byte b[] = new byte[1024];
int n;
while ((n = in.read(b,0,1024)) >= 0) {
  out.write(b,0,n);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何加快 Java / Android 中的解压时间? 的相关文章

随机推荐