Android文件下载,解压zip文件,清空文件夹

//保存retrofit下载下来的ResponseBody文件
 public static File writeResponseBodyToDisk(okhttp3.ResponseBody body, String name) {
        try {
            File myDir = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "YourRootFile");
            if (!myDir.exists()) {
                myDir.mkdirs();
            }
            File file = new File(myDir.getAbsolutePath(), name);
            LogUtil.i("file download: " + file.getAbsolutePath());
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                byte[] buffer = new byte[4096];
                inputStream = body.byteStream();
                outputStream = new FileOutputStream(file);
                int read;
                while ((read = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, read);
                }
                outputStream.flush();
                return file;
            } catch (IOException e) {
                return file;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return null;
        }
    }
//解压下载的zip包
    public static void unZipFolder(String archive, String decompressDir) {
        try {
            BufferedInputStream bi;
            ZipFile zf = new ZipFile(archive);
            Enumeration e = zf.entries();
            while (e.hasMoreElements()) {
                ZipEntry ze2 = (ZipEntry) e.nextElement();
                String entryName = ze2.getName();
                String path = decompressDir + "/" + entryName;
                if (ze2.isDirectory()) {
                    LogUtil.i("正在创建解压目录 - " + entryName);
                    File decompressDirFile = new File(path);
                    if (!decompressDirFile.exists()) {
                        decompressDirFile.mkdirs();
                    }
                } else {
                    LogUtil.i("正在创建解压文件 - " + entryName);
                    String fileDir = path.substring(0, path.lastIndexOf("/"));
                    File fileDirFile = new File(fileDir);
                    if (!fileDirFile.exists()) {
                        fileDirFile.mkdirs();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(decompressDir + "/" + entryName));
                    bi = new BufferedInputStream(zf.getInputStream(ze2));
                    byte[] readContent = new byte[1024];
                    int readCount = bi.read(readContent);
                    while (readCount != -1) {
                        bos.write(readContent, 0, readCount);
                        readCount = bi.read(readContent);
                    }
                    bos.close();
                }
            }
            zf.close();
        } catch (IOException e) {
            LogUtil.i("faile to unzip file");
        }
    }
//清空下载的文件
    public static void clearFile(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }
        if (file.isDirectory()) {
            File[] childFile = file.listFiles();
            if (childFile == null || childFile.length == 0) {
                file.delete();
                return;
            }
            for (File f : childFile) {
                clearFile(f);
            }
            //file.delete();
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用上面提到的 QDir 类来获取文件夹中所有文件,然后对其中的 zip 文件进行解压缩。这里给出一个示例代码: ```cpp #include <QDir> #include <QFileInfo> #include <QZipReader> #include <QDebug> void extractZipFile(const QString& filePath) { QZipReader zipReader(filePath); if (!zipReader.isOpen()) { qWarning() << "Failed to open zip file:" << filePath; return; } // 获取 zip 文件中所有文件 const QList<QZipReader::FileInfo> files = zipReader.fileInfoList(); foreach (const QZipReader::FileInfo& fileInfo, files) { if (fileInfo.isFile()) { // 解压文件 const QString fileName = fileInfo.filePath(); const QString absoluteFilePath = QDir::toNativeSeparators(QDir::currentPath() + QDir::separator() + fileName); if (!zipReader.extractFile(fileName, absoluteFilePath)) { qWarning() << "Failed to extract file:" << fileName; } } } zipReader.close(); } void traverseFolder(const QString& folderPath) { QDir dir(folderPath); dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::Dirs); QFileInfoList fileInfoList = dir.entryInfoList(); foreach (QFileInfo fileInfo, fileInfoList) { if (fileInfo.isFile()) { const QString filePath = fileInfo.absoluteFilePath(); if (filePath.endsWith(".zip")) { extractZipFile(filePath); } // 这里可以对其它文件进行进一步处理,例如输出、存储等操作 } else if (fileInfo.isDir()) { const QString subFolder = fileInfo.absoluteFilePath(); traverseFolder(subFolder); } } } int main(int argc, char* argv[]) { traverseFolder("your/folder/path"); return 0; } ``` 这段代码首先使用 traverseFolder() 函数来遍历文件夹及其子目录,查找所有的 zip 文件,并调用 extractZipFile() 函数来解压缩每个 zip 文件。extractZipFile() 函数使用 QZipReader 类来打开 zip 文件并获取其中的文件,然后使用 extractFile() 方法来解压缩其中的每个文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值