如何使用c#以写模式打开密码保护的Zip文件?
我已经使用ionic.zip.dll使用zip保护csv文件 请参阅下面的代码如何使用c#以写模式打开密码保护的Zip文件?,c#,csv,zipfile,C#,Csv,Zipfile,我已经使用ionic.zip.dll使用zip保护csv文件 请参阅下面的代码 using (ZipFile zip = new ZipFile()) { TargetFileName = TargetNamePrefix; OutPutDirectoryPath = TargetLocation + "\\" + TargetNamePrefix + ".zip"; zip.Password = zipFilePassword; z
using (ZipFile zip = new ZipFile())
{
TargetFileName = TargetNamePrefix;
OutPutDirectoryPath = TargetLocation + "\\" + TargetNamePrefix + ".zip";
zip.Password = zipFilePassword;
zip.AddFiles(FilePaths, TargetNamePrefix);
zip.Save(OutPutDirectoryPath)
}
这里的文件路径是字符串[]变量,它由文件(csv/文本)名称组成。
TargetNamePrefix表示zipfile文件夹名称内的名称。OutPutDirectoryPath表示
具有zipfileName的输出方向。
那么我如何才能在写模式下打开这些受保护的文件?为什么?因为我想将数据写入受保护的csv文件 您可以使用类似的方法提取它们:
using(ZipFile zip = ZipFile.Read(OutPutDirectoryPath))
{
zip.Password = zipFilePassword;
try
{
zip.ExtractAll(TargetLocation);
}
catch (BadPasswordException e)
{
// handle error
}
}
更新
访问流中的单个文件条目而不提取
string entry_name = TargetNamePrefix + ".csv"; // update this if needed
using (ZipFile zip = ZipFile.Read(OutPutDirectoryPath))
{
// Set password for file
zip.Password = zipFilePassword;
// Extract entry into a memory stream
ZipEntry e = zip[entry_name];
using(MemoryStream m = new MemoryStream())
{
e.Extract(m);
// Update m stream
}
// Remove old entry
zip.RemoveEntry(entry_name);
// Add new entry
zip.AddEntry(entry_name, m);
// Save
zip.Save(OutPutDirectoryPath);
}
对不起,可能是重复的。。。我对提取不感兴趣,因为我想保护数据不受外界影响,只是我需要在写入模式下从ZIP文件打开所需文件请帮助我破解此…@Pavan-请参阅更新,让我知道这是否适用于您。实际上,我需要打开密码保护zip文件中可用的csv/txt文件。我必须使用SreamWriter类将数据写入该文件。您可以帮助我解决此问题吗..我可以使用StreamWriter类打开密码zip文件吗在上述代码中删除现有文件并添加相同的新文件,但我只需要在现有文件中写入数据。你能帮我解决这个问题吗。。。