我在我的Pocket PC项目中使用Ionic.Zip(Compact Framework版本)
解压缩压缩文件(使用Ionic.Zip)工作正常。如果我把密码放在压缩文件上,它在提取之前需要密码,但当我尝试这个实例时,提取的密码验证失败。
示例:此文件夹即将压缩。
MyDeviceMy DocumentsMy Pictures
此文件夹包含两个文件('Flower.jpg','Waterfall.jpg')
使用以下代码压缩文件:
public string Compress(string[] Paths, string SaveFileName, string Password, string CompressionType)
{
try
{
using (ZipFile zip = new ZipFile())
{
if (string.IsNullOrEmpty(Password))
zip.Password = Password;
zip.CompressionLevel = Utility.GetCompressionLevel(CompressionType);
foreach (string item in Paths)
{
if (Utility.IsDirectory(item))
zip.AddDirectory(item);
else if (Utility.IsFile(item))
zip.AddFile(item);
}
if (!SaveFileName.Trim().ToLower().EndsWith(".zip"))
if (SaveFileName.Trim().EndsWith("."))
SaveFileName += "zip";
else
SaveFileName += ".zip";
zip.Save(SaveFileName);
}
return Utility.GetResourceString("ZipSuccess");
}
catch (Exception ex)
{
return ex.Message;
}
}
提取代码:
public string Decompress(string ZipFilePath, string TargetPath, string Password, bool OverwriteExistingFiles)
{
try
{
using (ZipFile decompress = ZipFile.Read(ZipFilePath))
{
if (!string.IsNullOrEmpty(Password))
decompress.Password = Password;
foreach (ZipEntry e in decompress)
{
e.Extract(TargetPath, OverwriteExistingFiles ? ExtractExistingFileAction.OverwriteSilently : ExtractExistingFileAction.DoNotOverwrite);
}
}
return Utility.GetResourceString("ExtractSuccess");
}
catch (Exception ex)
{
return ex.Message;
}
}
在这个位置提取文件效果很好,因为它需要密码:
MyDeviceMy DocumentsPersonal
但是!当我提取同一文件夹上的文件时:
MyDeviceMy DocumentsMy Pictures
它在不需要密码的情况下提取文件
我认为这是一个bug。我能为此做些什么
希望有人能回答。谢谢
您的Compress()方法中有一个错误。压缩文件时,ZipFile实例的Password属性从未设置。看看您决定是否分配zip的逻辑。密码属性。
上面写着:
if (string.IsNullOrEmpty(Password))
zip.Password = Password;
正如所写的,拉链。只有当Password参数为null或为空字符串时,才会设置Password属性。如果Password参数是非空字符串,则代码跳过zip。密码分配语句。
Compress()方法中的if语句缺少而非运算符。它应该是:
if ( ! string.IsNullOrEmpty(Password))
zip.Password = Password