如何使用 Poco::ZIP 压缩/解压缩 zip 文件
How to use Poco::ZIP to compress/decompress zip file
我是C++新手,尤其是在压缩/解压缩方面。我当前的项目正在使用 Poco 库,Zip 扩展名用于将文件/目录压缩或解压缩为 Zip 文件。
#include <Poco/FileStream.h>
#include <Poco/Zip/Decompress.h>
#include <Poco/Zip/Compress.h>
#include <Poco/Timestamp.h>
#include <Poco/File.h>
voidZipFile(string source, string target, List extensions, bool append, bool overWrite)
{
Poco::DateTime(Timestamp);
set <string> extensionsSet;
std::ofstream fos(target, ios::binary);
Poco::Zip::Compress c(fos, true);
for (int i = 0; i < extensions.Size(); i++) {
string ext = std::dynamic_pointer_cast<String>(extensions.operator[](i))->GetPrimitive();
extensionsSet.insert(ext);
}
c.setStoreExtensions(extensionsSet);//set extensions List
Poco::File aFile(source);//This is where I start my compress action
if (aFile.exists())
{
Poco::Path p(aFile.path());
if (aFile.isDirectory())
{
if (p.isDirectory()) {
c.addDirectory(p, Poco::DateTime());
}
else {
}
}
else if (aFile.isFile())
{
c.addFile(p, p.getFileName());
}
}
else {
_log.EndMethod();
throw new FileNotFoundException("File Not Found");
}
//Poco::FileOutputStream fos(target, std::ios::binary);
c.close(); // MUST be done to finalize the Zip file
fos.close();
}
上面的代码是我目前所拥有的,我可以将单个文件压缩为.zip
文件。
如何将文件夹/目录压缩为.zip文件?我不能使用另一个库,因为 Poco 也用于我当前项目的其他部分。
您需要添加递归方法来搜索文件夹。
这是我的想法:
Poco::File aFile(entry);
if (!aFile.isDirectory())
throw ZipException("Not a directory: "+ entry.toString());
Poco::Path aName(name);
aName.makeDirectory();
if (!excludeRoot)
{
if (aName.depth() == 0)
{
Poco::Path tmp(entry);
tmp.makeAbsolute(); // eliminate ../
aName = Poco::Path(tmp[tmp.depth()-1]);
aName.makeDirectory();
}
addDirectory(aName, aFile.getLastModified());
}
// iterate over children in the directory
std::vector<std::string> children;
aFile.list(children);
std::vector<std::string>::const_iterator it = children.begin();
std::vector<std::string>::const_iterator itEnd = children.end();
for (; it != itEnd; ++it)
{
Poco::Path realFile(entry, *it);
Poco::Path renamedFile(aName, *it);
Poco::File aFile(realFile);
if (aFile.isDirectory())
{
realFile.makeDirectory();
renamedFile.makeDirectory();
addRecursive(realFile, cm, cl, false, renamedFile);
}
else
{
realFile.makeFile();
renamedFile.makeFile();
addFile(realFile, renamedFile, cm, cl);
}
}
相关文章:
- 使用C++进行运行长度解压缩
- C++ 如何将数组值解压缩为函数参数
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- 浏览压缩文件与游览解压缩它们
- 如何在C++向量中解压缩多个值
- 解压缩 C 样式数组以及C++中的参数包
- 如何在 cpp 中解压缩数字,如果它们是使用 struct.pack(fmt, v1, v2, ..) 打包在 pyth
- 如何使用 Poco::ZIP 压缩/解压缩 zip 文件
- 使用迷你zip解压缩字符数组
- 如何在视觉工作室中使用C++解压缩压缩文件 (.zip)
- 如何使用 zlib.h 库将.zip文件解压缩到文件夹中
- 使用 zlib 解压缩.zip文件的简单方法
- 从C++解压缩 ZIP 的最简单方法
- 寻找可以解压缩rar,7z,bz2,zip等的c ++压缩库
- 有没有关于如何将包含文件夹和文件的zip文件解压缩到某个目录并通过MiniZip将其打包的教程
- 使用zlib/minizip:C/C++应用程序解压缩zip文件
- (C++)解压缩 0 字节的文件时ZR_FLATE错误。解压缩源:"Zip Utils -clean, elegant...."
- C++-Zip Utils从内存解压缩到内存
- Zlib解压缩.zip文件