Extracting files from a Zip archive programmatically using C# and System. Here's an example to create a zip. Unpacking zip file in c# using System.IO.Packaging. System.IO.Packaging ZipPackage Class. /// <summary> /// Creates a package zip file containing specified. // Create the Package // (If the package file. System.IO.Packaging. /// <summary> /// Creates a package zip file containing specified /// content and. // Create the Package // (If the package file. Creating a folder inside the ZIP file. how to create a directory inside that zip file to. folder inside the ZIP file with System.IO.Packaging.
. System.IO.Packaging. . so that we could directly create and access Zip files without pretending we. to zip with dll in c#.net.
Zip. Package Class (System. IO. Packaging)This example shows how to create a basic Zip. Package. The example creates a package that contains a single document part which is defined as the package's root element by a package- level Package. Relationship. The package also contains an image part and a second Package. Relationship which defines an association between the source document part and the target image part. The image is a resource that is used with the document).
Create. Package - -- -- -- -- -- -- -- -- -- -- -- -- -///< summary> /// Creates a package zip file containing specified/// content and resource files.< /summary> privatestaticvoid Create. Package(). // Convert system path and file names to Part URIs. In this example// Uri part. Uri. Document /* /Content/Document. Pack. Uri. Helper. Create. Part. Uri(// new Uri("Content\Document. Uri. Kind. Relative)); // Uri part.
- I'm trying to use C# to decompress a zip file so that I can read the data in a. decrompress a zip file using System.IO.Packaging. FileMode.Create.
- This lets you create. Zip Your Streams with System.IO.Packaging. hence i thought of doing it using system.io.packaging which ll zip the files and store.
- Using System.IO.Packaging to generate a ZIP file. not impossible to use System.IO.Packaging to generate a ZIP file. Creating zip in c# with System.IO.Packaging.
- System.IO.Packaging uses the ZIP file format as its. Using System.IO.Packaging to ZIP your files. ZipArchive to create, read and extract Zip files.
Uri. Resource /* /Resources/Image. Pack. Uri. Helper.
Create. Part. Uri(// new Uri("Resources\Image. Uri. Kind. Relative)). Uri part. Uri. Document = Pack. Uri. Helper. Create. Part. Uri(. new Uri(document. Path, Uri. Kind. Relative)). Uri part. Uri. Resource = Pack.
Uri. Helper. Create. Part. Uri(. new Uri(resource. Path, Uri. Kind. Relative)). Create the Package// (If the package file already exists, File. Mode. Create will// automatically delete it first before creating a new one.// The 'using' statement insures that 'package' is// closed and disposed when it goes out of scope.)using (Package package =. Package. Open(package.
Path, File. Mode. Create)). // Add the Document part to the Package. Package. Part package. Part. Document =. Create. Part(part.
Uri. Document. System. Net. Mime. Media. Type. Names. Text. Xml). // Copy the data to the Document Partusing (File. Stream file. Stream = new File. Stream(. document.
Path, File. Mode. Open, File. Access.
Read)). Copy. Stream(file. Stream, package. Part.
Document. Get. Stream()). Stream) - Close and dispose file. Stream.// Add a Package Relationship to the Document Part. Create. Relationship(package.
Part. Document. Uri. Target. Mode. Internal. Package. Relationship. Type). // Add a Resource Part to the Package. Package. Part package.
Part. Resource =. Create. Part(part.
Uri. Resource. System. Net. Mime. Media. Type. Names. Image. Jpeg). // Copy the data to the Resource Partusing (File. Stream file. Stream = new File. Stream(. resource.
Path, File. Mode. Open, File. Access. Read)). Copy. Stream(file. Stream, package. Part. Resource. Get. Stream()). Stream) - Close and dispose file.
Stream.// Add Relationship from the Document part to the Resource part. Part. Document. Create. Relationship(. new Uri(@"./resources/image. Uri. Kind. Relative). Target. Mode. Internal. Resource. Relationship. Type). }// end: using (Package package) - Close and dispose package.
Create. Package()// - -- -- -- -- -- -- -- -- -- -- -- -- -- Copy. Stream - -- -- -- -- -- -- -- -- -- -- -- -- -- ///< summary> /// Copies data from a source stream to a target stream.< /summary> ///< param name="source"> /// The source stream to copy from.< /param> ///< param name="target"> /// The destination stream to copy to.< /param> privatestaticvoid Copy. Stream(Stream source, Stream target). Size = 0x. 10. 00. Size]. int bytes. Read = 0. while ((bytes.
Read = source. Read(buf, 0, buf. Size)) > 0). target. Write(buf, 0, bytes. Read). }// end: Copy. Stream()For the complete sample, see Writing a Package Sample.
Using System. IO. Packaging to ZIP your files - . NET Development. System.
IO. Packaging provides storage support for multiple data objects in a single container. System. IO. Packaging uses the ZIP file format as its primary physical format for a Package. Other Package implementations might use an XML document, a database, or Web service. The following contains a utility class I wrote to help zip a file or a folder into a ZIP archive.
This is an asynchronous as is suitable for invoking from a Win Forms or a WPF Application when we need to display a progress bar. Here is the code: using System; using System. IO; using System. IO. Packaging; using System. Linq; using System.
Web; using Inverted. Software. Common. Events; namespace Inverted. Software. IO{public delegate void Zip. Event. Handler(object sender, Zip. Event. Args e); public delegate void Zip. End. Event. Handler(object sender, Zip.
End. Event. Args e); /// < summary> /// Utility class for Zipping and Unzipping files and folders into XPS Packages/// This class uses XML Paper Specification and adds [Content_Types]. Packaging{ #region Eventsprivate delegate void Async. Start. Package. Folder(string folder. Name, string compressed. File. Name, bool override.
Existing); private delegate void Async. Start. Package. File(string file. Name, string compressed. File. Name, bool override. Existing); /// < summary> /// Add a folder along with its subfolders to a Package Asynchronously/// < /summary> /// < param name="folder.
Name"> The folder to add< /param> /// < param name="compressed. File. Name"> The package to create< /param> /// < param name="override. Existing"> Override exsisitng files< /param> public void Package. Folder. Async(string folder. Name, string compressed.
File. Name, bool override. Existing){Async. Start. Package. Folder method.
Call = new Async. Start. Package. Folder(Start. Package. Folder); method. Call. Begin. Invoke(folder. Name,compressed. File.
Name,override. Existing,new Async. Callback(Run. Zip. Complete),compressed.
File. Name); } /// < summary> /// Compress a file into a ZIP archive as the container store Asynchronously/// < /summary> /// < param name="file. Name"> The file to compress< /param> /// < param name="compressed.
File. Name"> The archive file< /param> /// < param name="override. Existing"> override existing file< /param> public void Package. File. Async(string file. Name, string compressed. File. Name, bool override. Existing){Async. Start.
Package. Folder method. Call = new Async. Start. Package. Folder(Start.
Package. File); method. Call. Begin. Invoke(file. Name,compressed. File.
Name,override. Existing,new Async. Callback(Run. Zip.
Complete),compressed. File. Name); } private void Start. Package. Folder(string folder. Name, string compressed.
File. Name, bool override. Existing){Package.
Folder(folder. Name, compressed. File. Name, override. Existing); } private void Start. Package. File(string file. Name, string compressed.
File. Name, bool override. Existing){Package. File(file. Name, compressed.
File. Name, override. Existing); } private void Run. Zip. Complete(IAsync. Result result){ } public event Zip.
Event. Handler Zip; protected virtual void On. Zip(Zip. Event. Args e){if (Zip != null)Zip(this, e); } public event Zip. End. Event. Handler Zip. End; protected virtual void On. Zip. End(Zip. End. Event. Args e){if (Zip.
End != null)Zip. End(this, e); } #endregion private const string Package. Relationship. Type = @"http: //schemas. Resource. Relationship. Type = @"http: //schemas. Add a folder along with its subfolders to a Package/// < /summary> /// < param name="folder. Name"> The folder to add< /param> /// < param name="compressed. File. Name"> The package to create< /param> /// < param name="override.
Existing"> Override exsisitng files< /param> /// < returns> Return. Result< /returns> public Return. Result Package. Folder(string folder. Name, string compressed. File. Name, bool override.
Existing){if (folder. Name. Ends. With(@"\"))folder. Name = folder. Name.
Remove(folder. Name. Length - 1); Return. Result result = new Return. Result(); if (! Directory. Exists(folder. Name)){result.
Message = "Source folder doesnt exist in" + folder. Name; return result; } if (! Existing & & File. Exists(compressed. File. Name)){result. Message = "Destination file " + compressed.
File. Name + " cannot be overwritten"; return result; }try{using (Package package = Package. Open(compressed. File. Name, File. Mode. Create)){var file. List = Directory. Enumerate. Files(folder.
Name, "*", Search. Option. All. Directories); Zip. Event. Args zip. Args = new Zip. Event. Args() { Total. Files = file. List. Count(), File. Number = 0, Bytes.
Zipped = 0, Total. Bytes = 0 }; foreach (string file. Name in file. List){zip. Args. File. Number++; if (Path.
Get. File. Name(file. Name). Index. Of. Any(Path. Get. Invalid. File. Name. Chars()) > - 1)continue; //The path in the package is all of the subfolders after folder. Namestring path. In. Package; path. In.
Package = Path. Get. Directory. Name(file. Name). Replace(folder. Name, string. Empty) + "/" + Path. Get. File. Name(file. Name); Uri part. Uri. Document = Pack.
Uri. Helper. Create. Part. Uri(new Uri(path. In. Package, Uri. Kind. Relative)); Package. Part package. Part. Document = package.
Create. Part(part. Uri. Document, System. Net. Mime. Media. Type. Names. Text. Xml, Compression. Option. Maximum); using (File.
Stream file. Stream = new File. Stream(file. Name, File.
Mode. Open, File. Access. Read)){file. Stream. Copy. To(package. Part. Document. Get.
Stream()); zip. Args. Bytes. Zipped += file. Stream. Length; On. Zip(zip. Args); }package.
Create. Relationship(package. Part. Document. Uri, Target. Mode. Internal, Package. Relationship. Type); }}}catch (Exception e){throw new Exception("Error zipping folder " + folder.
Name, e); }On. Zip. End(new Zip. End. Event. Args()); result.
Success = true; result. Message = "OK"; return result; } /// < summary> /// Compress a file into a ZIP archive as the container store/// < /summary> /// < param name="file. Name"> The file to compress< /param> /// < param name="compressed. File. Name"> The archive file< /param> /// < param name="override. Existing"> override existing file< /param> /// < returns> Return.
Result< /returns> public Return. Result Package. File(string file. Name, string compressed.
File. Name, bool override. Existing){Return. Result result = new Return. Result(); if (! File. Exists(file.
Name)){result. Message = "Source file doesnt exist in" + file. Name; return result; } if (! Existing & & File. Exists(compressed.
File. Name)){result. Message = "Destination file " + compressed.
File. Name + " cannot be overwritten"; return result; } try{Uri part. Uri. Document = Pack. Uri. Helper. Create. Part. Uri(new Uri(Path.
Get. File. Name(file. Name), Uri. Kind. Relative)); using (Package package = Package. Open(compressed. File. Name, File. Mode. Create)){Package.
Part package. Part. Document = package.
Create. Part(part. Uri. Document, System. Net. Mime. Media. Type. Names. Text. Xml, Compression.
Option. Maximum); using (File. Stream file. Stream = new File. Stream(file. Name, File.
Mode. Open, File. Access. Read)){Zip. Event. Args zip. Args = new Zip. Event. Args() { Total. Files = 1, File. Number = 1, Total. Bytes = new File. Info(file. Name).
Length, Bytes. Zipped = 0 }; On. Zip(zip. Args); file. Stream. Copy. To(package. Part. Document. Get. Stream()); zip. Args.
Bytes. Zipped += file. Stream. Length; On.
Zip(zip. Args); }package. Create. Relationship(package. Part. Document. Uri, Target.
Mode. Internal, Package. Relationship. Type); }}catch (Exception e){throw new Exception("Error zipping file " + file. Name, e); }On. Zip. End(new Zip. End. Event. Args()); result. Success = true; result. Message = "OK"; return result; } /// < summary> /// Extract a container Zip. NOTE: container must be created as Open Packaging Conventions (OPC) specification./// Delete [Content_Types].
Name"> The folder to extract the package to< /param> /// < param name="compressed. File. Name"> The package file< /param> /// < param name="override. Existing"> override existing files< /param> /// < returns> Return. Result< /returns> public Return. Result Uncompress. File(string folder.
Name, string compressed. File. Name, bool override. Existing){return Uncompress. File(folder. Name, compressed.
File. Name, override. Existing, true); } /// < summary> /// Extract a container Zip. NOTE: container must be created as Open Packaging Conventions (OPC) specification/// < /summary> /// < param name="folder. Name"> The folder to extract the package to< /param> /// < param name="compressed. File. Name"> The package file< /param> /// < param name="override.
Existing"> override existing files< /param> /// < param name="remove. Desc. File"> Delete [Content_Types]. Return. Result< /returns> public Return. Result Uncompress. File(string folder. Name, string compressed.
File. Name, bool override. Existing, bool remove. Desc. File){Return.
Result result = new Return. Result(); try{if (! File. Exists(compressed. File. Name)){result.
Success = false; result. Message = "Compressed File not found"; return result; } Directory. Info directory. Info = new Directory. Info(folder. Name); if (! Info. Exists)directory. Info. Create(); using (Package package = Package.
Open(compressed. File. Name, File. Mode. Open, File. Access. Read)){Package. Part document. Part = null; Package. Part resource. Part = null; Uri uri. Document. Target = null; foreach (Package.
Relationship relationship in package. Get. Relationships. By. Type(Package. Relationship. Type)){uri.