• 文件的常用操作(读取压缩文件、解压、删除)


    背景:最近做一个腾讯 cos 桶 文件的读写与本地数据库查询等操作
    Retrofit 中文件下载的可以添加 @Streaming

        @Streaming
        @GET
        Observable<ResponseBody> downloadCosFile(@Url String downloadUrl);
    
    • 1
    • 2
    • 3

    @Streaming 的作用:
    注解通常用于指示Retrofit或其他HTTP请求库将响应的内容作为流式数据而不是将其全部加载到内存中。这对于处理大文件或流式传输非常有用,因为它可以减少内存占用并提高性能。

    HttpCenter.getInstance().requestResponse(HttpCenter.getInstance().getApi().downloadCosFile(fileDownloadPath), new HttpObserver<ResponseBody>(ApiService.GET_COS_FILE_INFO) {
                @Override
                protected void onFailure(ApiException e) {
                    NLog.i(TAG, "onFailure:  cos 文件 请求失败" + e.getMessage());
                    callBack.onResult(null);
                }
    
                @Override
                protected void onSuccess(ResponseBody responseBody) {
                    NLog.i(TAG, "onSuccess:  cos 文件 请求成功");
                    // 下载文件
                    AppExecutors.autoExecute(() -> {
                    // 读入请求体的输入流
                        InputStream inputStream = responseBody.byteStream();
                        // 指定文件全路径-》 引申获取Android 几种路径的方式
                         String zipFilePath = "fileObsolutePath" 
                          File outputFile = new File(zipFilePath); // 保存到应用的私有目录
                          
                        try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
                            byte[] buffer = new byte[4 * 1024];
                            int read;
    
                            while ((read = inputStream.read(buffer)) != -1) {
                                outputStream.write(buffer, 0, read);
                            }
                            outputStream.flush();
                            NLog.i(TAG, "onSuccess: 文件写入成功");
                            String unZipFileDir;
                            
                            ZipUtils.UnZipFolder(outputFile.getAbsolutePath(), unZipFileDir, unZipPathList -> {
                                if (unZipPathList != null && unZipPathList.size() > 0) {
                                    // 每个压缩文件下只有一个文件,所以直接取第一个解压文件路径
                                    NLog.i(TAG, "onSuccess: 解压成功 " + unZipPathList.get(0));
                                    callBack.onResult(unZipPathList.get(0));
                                } else {
                                    callBack.onResult(null);
                                }
                            });
                            // 删除所有解析数据
                            deleteDirectory(FileUtil.getSecondFolder(DOWNLOAD_COS_DIR));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }});
                        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    ZipUtils.java 解压文件

    public static void UnZipFolder(String zipFileString, String outPathString, ICommListener<List<String>> iCommListener) throws IOException {
            ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
            ZipEntry zipEntry;
            String szName = "";
            List<String> unZipPathList = new ArrayList<>();
            while ((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();
                NLog.i(TAG, "UnZipFolder: zipEntry " + szName);
                if (zipEntry.isDirectory()) {
                    NLog.i(TAG, "UnZipFolder: 解压目录 ");
                    //获取部件的文件夹名
                    szName = szName.substring(0, szName.length() - 1);
                    File folder = new File(outPathString + File.separator + szName);
                    folder.mkdirs();
                } else {
                    NLog.i(TAG, "UnZipFolder: 解压文件 ");
                    String unZipPath = outPathString + File.separator + szName;
                    File file = new File(unZipPath);
                    if (!file.exists()) {
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                    }
                    // 获取文件的输出流
                    FileOutputStream out = new FileOutputStream(file);
                    int len;
                    byte[] buffer = new byte[1024];
                    // 读取(字节)字节到缓冲区
                    while ((len = inZip.read(buffer)) != -1) {
                        // 从缓冲区(0)位置写入(字节)字节
                        out.write(buffer, 0, len);
                        out.flush();
                    }
                    out.close();
                    unZipPathList.add(unZipPath);
                    NLog.i(TAG, "UnZipFolder: 解压文件写入完成 ");
                }
            }
            inZip.close();
            iCommListener.onResult(unZipPathList);
            NLog.i(TAG, "UnZipFolder: 解压结束");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    删除目录

     
        public static boolean deleteDir(final File dir) {
            if (dir == null) return false;
            // dir doesn't exist then return true
            if (!dir.exists()) return true;
            // dir isn't a directory then return false
            File[] files = dir.listFiles();
            if (files != null && files.length != 0) {
                for (File file : files) {
                    if (file.isFile()) {
                        if (!file.delete()) return false;
                    } else if (file.isDirectory()) {
                        if (!deleteDir(file)) return false;
                    }
                }
            }
            return dir.delete();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    探索跨境电商产品开发流程的最佳工具
    sqli-labs部分关思路
    华为OD机试 - 螺旋数字矩阵
    微服务系列之授权认证(一) OAuth 2.0 和 OpenID Connect
    『 C++类与对象』继承
    中国电子学会2023年09月份青少年软件编程Python等级考试试卷五级真题(含答案)
    MySQL的索引
    leetcode606. 根据二叉树创建字符串(java)
    Sqoop (四) --------- 配置解析简明版
    如何用Postman做接口自动化测试
  • 原文地址:https://blog.csdn.net/weixin_37716758/article/details/134045889