工作中遇到下载zip包到SDCard卡,然后使用ZipInputStream解压缩,解压没有问题,但是解压速度太慢了,文件有11M多,解压时间超长,如果需要解压的包更多的话担心程序会跑死。
大家有什么更好的解决方法吗?Android压缩zip速度

解决方案 »

  1.   

    不行就起个线程放后台解去吧,让用户先干点别的,解完了发个handler告诉主线程可以用了。。
      

  2.   


    public class UnZip {
    private Handler mHandler;

    public UnZip(Handler mHandler) {
    this.mHandler = mHandler;
    }

    public void unZipOffline(String from, String to) throws IOException {

    File fromFile = new File(from);
    File toFile = new File(to);

    FileInputStream fInputS = new FileInputStream(fromFile);

    ZipInputStream zipInput = new ZipInputStream(fInputS);
    // 获取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
    // 当getNextEntry方法的返回值为null,则代表ZipInputStream中没有下一个ZipEntry,
    // 输入流读取完成;
    ZipEntry entry = zipInput.getNextEntry();
    File file;

    entry = zipInput.getNextEntry();
    while (entry != null) {
            if(entry.isDirectory()) {
             String name = entry.getName();
                    name = name.substring(0, name.length() - 1);
              
                    file = new File(toFile.getPath() + File.separator + name);
                    boolean a = file.mkdirs();
            } else {
             file = new File(toFile.getPath() + File.separator + entry.getName());
                    file.createNewFile();
                    FileOutputStream out = new FileOutputStream(file);
                    int b;
                    while ((b = zipInput.read()) != -1) {
                        out.write(b);
                    }
                    out.close();
            }
            
            entry = zipInput.getNextEntry();
        }

    mHandler.sendEmptyMessage(0);
    }

    /**
     * 解压缩一个文件
     *
     * @param zipFile 压缩文件
     * @param folderPath 解压缩的目标目录
     * @throws IOException 当解压缩过程出错时抛出
     */
    public void upZipFile(String zipPath, String folderPath) throws ZipException, IOException {

    File zipFile = new File(zipPath);

    File desDir = new File(folderPath);
        if (!desDir.exists()) {
            desDir.mkdirs();
        }
        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
         ZipEntry entry = ((ZipEntry)entries.nextElement());
            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }
            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[4096];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
        
        mHandler.sendEmptyMessage(0);
    }这两个方法解压效率差别很大,第二个很快就好了,请教下这是为什么
      

  3.   

    方法一 while ((b = zipInput.read()) != -1) {
                        out.write(b);
                    }
    一个byte一个byte地写方法二byte buffer[] = new byte[4096];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }4KB、4KB地写效率自然差得远