SharpZipLib
SharpZipLib copied to clipboard
Attempting to read txt text inside a zip file with a password set, but getting an error
Original title: 在设置了密码的zip文件内尝试读取txt文本,但会遇到一个错误
Steps to reproduce
IEnumerator GetZipStr(string zipPath, string fileName)
{
string data = string.Empty;
ZipInputStream zip = new ZipInputStream(File.OpenRead(zipPath));
FileStream filestream = new FileStream(zipPath, FileMode.Open, FileAccess.Read);
ZipFile zipfile = new ZipFile(filestream);
zipfile.Password = "1234";
ZipEntry item;
while ((item = zip.GetNextEntry()) != null)
{
if (item.Name == fileName)
{
using (StreamReader s = new StreamReader(zipfile.GetInputStream(item)))
{
data = s.ReadToEnd();
s.Dispose();
}
break;
}
yield return null;
}
}
Expected behavior
Read txt text in an encrypted zip file
(original: 在一个加密的zip文件读取txt文本)
Actual behavior
I get an error while reading, ZipException: No password set., but in fact I have provided the correct password
(original: 读取时遇到一个错误,ZipException: No password set.,但事实我已经提供了正确的密码)
Version of SharpZipLib 1.3.3
Obtained from (only keep the relevant lines)
- Package installed using NuGet
You are using both ZipFile and ZipInputStream:
ZipInputStream zip = new ZipInputStream(File.OpenRead(zipPath));
FileStream filestream = new FileStream(zipPath, FileMode.Open, FileAccess.Read);
ZipFile zipfile = new ZipFile(filestream);
Then you are providing the password to zipfile, but iterating using zip (the ZipInputStream), which has no password set.
Use either only ZipFile or only ZipInputStream. As you are reading from a file, the ZipFile class is probably better suited for your use case.