How to read zip file content?
How to read the 开发者_开发问答content of file from zip without decompressing ? After searching the file name in zip ,I want to extract the file in window temp folder and copy the file and then delete extract file. Please reply for my question .
You can use sharpziplib to read the file without writing it to disk. It can be done like this:
public string Uncompress(string zipFile, string entryName)
{
string s = string.Empty;
byte[] bBuffer = new byte[4096];
ZipInputStream aZipInputStream = null;
aZipInputStream = new ZipInputStream(File.OpenRead(zipFile));
ZipEntry anEntry;
while ((anEntry = aZipInputStream.GetNextEntry()) != null)
{
if (anEntry.Name == entryName)
{
MemoryStream aMemStream = new MemoryStream();
int bSize;
do
{
bSize = aZipInputStream.Read(bBuffer, 0, bBuffer.Length);
aMemStream.Write(bBuffer, 0, bSize);
}
while (bSize > 0);
aMemStream.Close();
byte[] b = aMemStream.ToArray();
s = Encoding.UTF8.GetString(b);
aZipInputStream.CloseEntry();
break;
}
else
aZipInputStream.CloseEntry();
}
if (aZipInputStream != null)
aZipInputStream.Close();
return s;
}
精彩评论