多线程解压缩。它可能在android?

时间:2017-03-24 11:50:36

标签: android multithreading unzip

我在我的应用程序中添加了解压缩功能,并注意到它解压缩得非常慢。可以用多组线程解压缩吗? PHONE 是三星S6 EDGE,这就是为什么我认为它可能会更快

使用@Streaming,OkHttp和UNZIP进行Retrofit2:

public interface FileService {

    @Streaming
    @GET
    Call<ResponseBody> downloadFile(@Url String fileUrl);
}

mFileService.downloadFile(mScheme.getmPath()).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            new AsyncTask<ResponseBody, Long, Boolean>() {
                @Override
                protected Boolean doInBackground(final ResponseBody... responseBodies) {

                    ResponseBody body = responseBodies[0];
                    long length = body.contentLength();
                    unzip(body);
                    return true;
                }

                @Override
                protected void onProgressUpdate(Long... values) {
                    super.onProgressUpdate(values);
                    pb.setProgress(values[0].intValue());
                    tv.setText(values[0].intValue()+"%");
                }

                @Override
                protected void onPostExecute(Boolean aBoolean) {
                    super.onPostExecute(aBoolean);
                    if(aBoolean){

                        realm.executeTransaction(new Realm.Transaction() {
                            @Override
                            public void execute(Realm realm) {
                                m.setDownloaded(true);
                                mScheme.setDownloaded(true);
                                mScheme.setmPath(mScheme.getmPath().substring(0,mScheme.getmPath().indexOf(".zip")));
                                //realm.insertOrUpdate(m);
                                //realm.insertOrUpdate(mScheme);
                            }
                        });
                        Log.d(TAG, mScheme.getmPath());
                    }
                }

                private void unzip(ResponseBody body) {

                    long fileSize = body.contentLength();
                    long fileSizeDownloaded = -1;

                    try  {
                        InputStream in = body.byteStream();
                        ZipInputStream zin = new ZipInputStream(in);
                        ZipEntry ze = null;
                        while ((ze = zin.getNextEntry()) != null) {
                            Log.v("Decompress", "Unzipping " + ze.getName());

                            if(ze.isDirectory()) {
                                _dirChecker(ze.getName());
                            } else {
                                FileOutputStream fout = new FileOutputStream(Constants.CONFIG.CACHE_LOCATION + ze.getName());
                                for (int c = zin.read(); c != -1; c = zin.read()) {
                                    publishProgress(fileSizeDownloaded * 100/fileSize);
                                    fileSizeDownloaded++;
                                    fout.write(c);
                                }

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

                        }
                        zin.close();
                        Log.d("Decompress", "done");
                    } catch(Exception e) {
                        Log.e("Decompress", "unzip", e);
                    }

                }

                private void _dirChecker(String dir) {
                    File f = new File(Constants.CONFIG.CACHE_LOCATION + dir);

                    if(!f.isDirectory()) {
                        f.mkdirs();
                    }
                }
            }.execute(response.body());

        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
        }

    });

1 个答案:

答案 0 :(得分:1)

此代码:

for (int c = zin.read(); c != -1; c = zin.read()) {
    publishProgress(fileSizeDownloaded * 100/fileSize);
    fileSizeDownloaded++;
    fout.write(c);
}

一次读取一个字节非常慢。这可能会加速:

byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = zin.read(buffer)) != -1) {
    publishProgress(fileSizeDownloaded * 100/fileSize);
    fileSizeDownloaded += bytesRead;
    fout.write(buffer, 0, bytesRead);
}

也许你可以通过为每个需要解压缩的文件调度一个单独的线程来并行化处理。