Zip文件下載解壓

Zip文件下載解壓

  1. Zip文件下載

項目中採用的是:

api 'com.liulishuo.okdownload:okdownload:1.0.5'
private static void startTask(final String mUrl, final String fileName) {
        startTime = System.currentTimeMillis();
        DownloadTask task = new DownloadTask.Builder(mUrl, new File(FileUtils.getOtherFolderPath()))
                .setFilename(fileName)
                // the minimal interval millisecond for callback progress
                .setMinIntervalMillisCallbackProcess(30)
                // do re-download even if the task has already been completed in the past.
                .setPassIfAlreadyCompleted(false)
                .build();
        task.enqueue(new DownloadListener4WithSpeed() {

            private long totalLength;
            private String readableTotalLength;

            @Override
            public void infoReady(@NonNull DownloadTask task, @NonNull BreakpointInfo info,
                                  boolean fromBreakpoint, @NonNull Listener4SpeedAssistExtend.Listener4SpeedModel model) {
                totalLength = info.getTotalLength();
                readableTotalLength = Util.humanReadableBytes(totalLength, true);

            }

            @Override
            public void progressBlock(@NonNull DownloadTask task, int blockIndex,
                                      long currentBlockOffset, @NonNull SpeedCalculator blockSpeed) {

            }

            @Override
            public void progress(@NonNull DownloadTask task, long currentOffset,
                                 @NonNull SpeedCalculator taskSpeed) {
                final String readableOffset = Util.humanReadableBytes(currentOffset, true);
                final String progressStatus = readableOffset + "/" + readableTotalLength;
                final String speed = taskSpeed.speed();
                final String progressStatusWithSpeed = progressStatus + "(" + speed + ")";

                Log.i(TAG, "Step 5: progress Speed: " + progressStatusWithSpeed + " | currentOffset " + currentOffset);
                //        CLog.cameraToFile(TAG, "Step 5: progress Speed: " + progressStatusWithSpeed);
            }

            @Override
            public void blockEnd(@NonNull DownloadTask task, int blockIndex, BlockInfo info,
                                 @NonNull SpeedCalculator blockSpeed) {

            }

            @Override
            public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause,
                                @Nullable Exception realCause, @NonNull SpeedCalculator taskSpeed) {
                if (cause == EndCause.COMPLETED) {
                    Log.i(TAG, "Step 6: down ok ");
                    //        CLog.cameraToFile(TAG, "Step 6: down ok ");
                    try {
                        endTime = System.currentTimeMillis() - startTime;
                        downTotal = endTime;
                        Log.i(TAG, "down file waster time: " + endTime);
                        // 開啓線程解壓圖片
                        ......
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void taskStart(@NonNull DownloadTask task) {

            }

            @Override
            public void connectStart(@NonNull DownloadTask task, int blockIndex,
                                     @NonNull Map<String, List<String>> requestHeaderFields) {

            }

            @Override
            public void connectEnd(@NonNull DownloadTask task, int blockIndex,
                                   int responseCode, @NonNull Map<String, List<String>> responseHeaderFields) {

            }
        });
    }
  1. Zip解壓
/**
     * 解壓zip到指定的路徑
     *
     * @param zipFileString ZIP的名稱
     * @param outPathString 要解壓縮路徑
     * @throws Exception
     */
    public static void unZipFolder(String zipFileString, String outPathString) throws Exception {
        // 1. 讀取 ZIP 文件格式的文件實現輸入流過濾器(解壓文件)
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));

        ZipEntry zipEntry;  // 2. ZipEntry:表示 ZIP 文件條目
        byte[] buffer = new byte[BUFFER]; // 3. 緩衝器
        int len; // 每次讀出來的長度
        Log.i(TAG, "------------------------------------------- ");

        String szName;
        // 4. getNextEntry 讀取下一個 ZIP 文件條目並將流定位到該條目數據的開始處
        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            Log.i(TAG, "before unZip Folder szName: " + szName);
            if (zipEntry.isDirectory()) {
                //獲取部件的文件夾名
                szName = szName.substring(0, szName.length() - 1);
//                Log.i(TAG, "------------------------------------------- ");

//                Log.i(TAG, "after unZip Folder szName: " + szName);
                File folder = new File(outPathString + File.separator + szName);
                boolean mkSuccess = folder.mkdirs();

                Log.i(TAG, szName + " directory create " + mkSuccess);

            } else {
//                Log.i(TAG, "********************************************");
                Log.e(TAG, outPathString + File.separator + szName);
                File file = new File(outPathString + File.separator + szName);
                if (!file.exists()) {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                // 獲取文件的輸出流
                FileOutputStream out = new FileOutputStream(file);

                // 讀取(字節)字節到緩衝區
                while ((len = inZip.read(buffer)) != -1) {
                    // 從緩衝區(0)位置寫入(字節)字節
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }
參考
  1. Java實現將文件或者文件夾壓縮成zip
  2. 利用java zip進行對文件的壓縮和解壓
  3. winzipaes開源項目 支持AES壓縮和解壓zip文件
  4. Java解壓和壓縮帶密碼的zip文件
  5. zip4j – Java處理zip壓縮文件的完整解決方案
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章