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压缩文件的完整解决方案
  6. Android中的断点下载 安卓断点下载
  7. Android实现多线程下载文件(支持暂停、取消、断点续传)
  8. OKDownload 下載框架 斷點續傳 MD
  9. Android 后台静默下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初心一点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值