Tuesday, February 9, 2016

How to extract one image from Archive and save it to disk c# ZipFile

ZipFile zf = new ZipFile(filename);

ZipEntry zipEntry = zf.GetEntry("ppt/media/image1.jpeg");

var entryFileName = zipEntry.Name;

// to remove the folder from the entry:
// entryFileName = Path.GetFileName(entryFileName);

byte[] buffer = new byte[4096]; // 4K is optimum
Stream zipStream = zf.GetInputStream(zipEntry);

// Manipulate the output filename here as desired.
String fullZipToPath = Path.Combine(@"C:\Temp", entryFileName);
string directoryName = Path.GetDirectoryName(fullZipToPath);

if (directoryName.Length > 0)
 Directory.CreateDirectory(directoryName);

using (FileStream streamWriter = File.Create(fullZipToPath))
{
 StreamUtils.Copy(zipStream, streamWriter, buffer);
}

No comments:

Post a Comment