使用 Poco::zip 添加新目录始终给出异常
Add new Directory with Poco::zip always give Exception
我正在构建一个压缩/解压缩函数,目前,我只能将 1 个单个文件压缩成一个 zip 文件。每次我添加新目录或递归添加目录时,它总是给我异常。
这是我的ZipFile
函数:
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);
if (aFile.exists())
{
Poco::Path p(aFile.path());
if (aFile.isDirectory())
{
Poco::Path sourceDir(source);
Poco::Path targetDir(target);
c.addDirectory(targetDir, Poco::DateTime(Timestamp));// give exception
targetDir.makeDirectory();
c.addRecursive(sourceDir);
}
else if (aFile.isFile())
{
c.addFile(p, p.getFileName());
}
}
else {
_log.EndMethod();
throw new FileNotFoundException("File Not Found");
}
c.close(); // MUST be done to finalize the Zip file
fos.close();
这是我的例外,下面的代码块在 Poco 的 addDirectory 方法中:
if (!ZipCommon::isValidPath(fileStr))
throw ZipException("Illegal entry name " + fileStr + " containing parent directory reference");
在"Poco::P ath"中有一个成员叫做"absolute"。 您可以通过构造函数将绝对设置为 false
否则会抛出 ZipException,因为此函数返回"false":
bool ZipCommon::isValidPath(const std::string& path)
{
try
{
if (Path(path, Path::PATH_UNIX).isAbsolute() || Path(path, Path::PATH_WINDOWS).isAbsolute())
return false;
}
...
}
下面是一个示例:
std::ofstream fos(target, ios::binary);
Poco::Zip::Compress compress(fos, true);
std::vector<std::string> dirs; // parent dirs in zip file
Poco::Path myPath(false); // set path to non-absolute
// add parent directories
for (auto& dir : dirs)
{
myPath.pushDirectory(dir);
}
//add file
myPath.setFileName("FileName.txt");
//add file to archive
compress.addFile(theFile, relativePath);
//close archive and get zipArchiveObject
ZipArchive zipArchive(compress.close());
相关文章:
- 为什么构建目录中新构建的共享库与安装目录中的副本具有不同的依赖项集?
- 如何将图像传入C++可执行文件并将输出图像存储在新目录中?
- 添加新行时工作代码引发异常.调试技巧?
- 使用 Poco::zip 添加新目录始终给出异常
- 将文件复制到 pro/pri 文件中的新目录
- 在 std::unordered_map 中插入新的键/值对会导致"out of range"异常
- 如何以异常安全的方式使用放置新?
- 我的数组大小调整代码在新数据上引发异常[size_t]
- C++读取添加到目录中的新文件
- C++通过继承 std::exception 来创建新的异常
- inotify:如何在传输完成后通知目录中的新文件
- 使用libcurl制作新目录
- 如何在 Linux 中的单独/包含目录中编译额外/更新的库以及新程序
- 全局关闭C++新运算符异常
- 将数据写入新文件时出现异常
- inotify-在目录树中的几个级别之后,无法将手表添加到新创建的目录中
- Win32 API从一个目录复制图片文件到新的/现有的目录
- 在SDL中创建一个新文件夹或目录
- VS Express 2013 -默认为每个新项目添加.h文件,使其位于相同的目录下
- 不会创建新目录