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

2023-09-12 03:02:35 作者:GD is king

在Android解压文件似乎是不堪缓慢。起初我还以为这只是仿真器,但它似乎是在手机上一样。我尝试过不同的COM pression水平,并最终下降到存储模式,但它仍然需要年龄。

总之,必须有一个理由!没有任何人有这个问题?我的解压方法是这样的:

 公共无效解压缩()
{
尝试{
        的FileInputStream鳍=新的FileInputStream(zip文件);
        ZipInputStream ZIN =新ZipInputStream(翅);
        文件rootfolder =新的文件(目录);
        rootfolder.mkdirs();
        ZipEntry的泽= NULL;
        而((泽= zin.getNextEntry())!= NULL){

            如果(ze.isDirectory()){
                dirChecker(ze.getName());
            }
            其他{
                FileOutputStream中FOUT =新的FileOutputStream(目录+ ze.getName());

            对于(INT C = zin.read(); C =  -  1;!C = zin.read()){
                fout.write(C);
            }
                //Debug.out("Closing流);
                zin.closeEntry();
                fout.close();

        }
    }
    zin.close();
}
赶上(例外五){
            //Debug.out("Error试图解压文件+压缩文件);

}
    }
 

解决方案

我不知道,如果解压的Andr​​oid是缓慢的,但复制字节的循环字节肯定慢下来,甚至更多。尝试使用的BufferedInputStream和的BufferedOutputStream - 它可能会更复杂一些,但以我的经验很值得中端

 的BufferedInputStream在=新的BufferedInputStream(ZIN);
的BufferedOutputStream OUT =新的BufferedOutputStream(FOUT);
 

然后你就可以用类似的东西写的:

  BYTE B [] =新的字节[1024];
INT N;
而((N = in.read(B,0,1024))> = 0){
  out.write(二,0,n)的;
}
 
Android UI 渲染机制的演进,你需要了解什么

Unzipping files on android seems to be dreadfully slow. At first I thought this was just the emulator but it appears to be the same on the phone. I've tried different compression levels, and eventually dropped down to storage mode but it still takes ages.

Anyway, there must be a reason! Does anyone else have this problem? My unzip method looks like this:

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);

}
    }

解决方案

I don't know if unzipping on Android is slow, but copying byte for byte in a loop is surely slowing it down even more. Try using BufferedInputStream and BufferedOutputStream - it might be a bit more complicated, but in my experience it is worth it in the end.

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

And then you can write with something like that:

byte b[] = new byte[1024];
int n;
while ((n = in.read(b,0,1024)) >= 0) {
  out.write(b,0,n);
}