Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO. Packaging;
Using System. IO;
Using Utilities. IO. ExtensionMethods;
Using Utilities. DataTypes. ExtensionMethods;
Namespace Utilities. FileFormats. Zip
{
/// <Summary>
/// Helper class for dealing with zip files
/// </Summary>
Public class ZipFile: IDisposable
{
# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "FilePath"> Path to the zip file </param>
/// <Param name = "Overwrite"> shocould the zip file be overwritten? </Param>
Public ZipFile (string FilePath, bool Overwrite = true)
{
FilePath. ThrowIfNullOrEmpty ("FilePath ");
ZipFileStream = new FileStream (FilePath, Overwrite? FileMode. Create: FileMode. OpenOrCreate );
}
# Endregion
# Region Properties
/// <Summary>
/// Zip file's FileStream
/// </Summary>
Protected virtual FileStream ZipFileStream {get; set ;}
# Endregion
# Region Functions
/// <Summary>
/// Adds a folder to the zip file
/// </Summary>
/// <Param name = "Folder"> Folder to add </param>
Public virtual void AddFolder (string Folder)
{
Folder. ThrowIfNullOrEmpty ("Folder ");
Folder = new DirectoryInfo (Folder). FullName;
If (Folder. EndsWith (@"\"))
Folder = Folder. Remove (Folder. Length-1 );
Using (Package = ZipPackage. Open (ZipFileStream, FileMode. OpenOrCreate ))
{
New DirectoryInfo (Folder)
. GetFiles ()
. ForEach (x => AddFile (x. FullName. Replace (Folder, ""), x, Package ));
}
}
/// <Summary>
/// Adds a file to the zip file
/// </Summary>
/// <Param name = "File"> File to add </param>
Public virtual void AddFile (string File)
{
File. ThrowIfNullOrEmpty ("File ");
FileInfo TempFileInfo = new FileInfo (File );
If (! TempFileInfo. Exists)
Throw new ArgumentException ("File ");
Using (Package = ZipPackage. Open (ZipFileStream, FileMode. OpenOrCreate ))
{
AddFile (TempFileInfo. Name, TempFileInfo, Package );
}
}
/// <Summary>
/// Uncompresses the zip file to the specified folder
/// </Summary>
/// <Param name = "Folder"> Folder to uncompress the file in </param>
Public virtual void UncompressFile (string Folder)
{
Folder. ThrowIfNullOrEmpty ("Folder ");
New DirectoryInfo (Folder). Create ();
Folder = new DirectoryInfo (Folder). FullName;
Using (Package = ZipPackage. Open (ZipFileStream, FileMode. Open, FileAccess. Read ))
{
Foreach (PackageRelationship Relationship in Package. GetRelationshipsByType ("http://schemas.microsoft.com/opc/2006/sample/document "))
{
Uri UriTarget = PackUriHelper. ResolvePartUri (new Uri ("/", UriKind. Relative), Relationship. TargetUri );
PackagePart Document = Package. GetPart (UriTarget );
Extract (Document, Folder );
}
If (File. Exists (Folder + @ "\ [Content_Types]. xml "))
File. Delete (Folder + @ "\ [Content_Types]. xml ");
}
}
/// <Summary>
/// Extracts an individual file
/// </Summary>
/// <Param name = "Document"> Document to extract </param>
/// <Param name = "Folder"> Folder to extract it into </param>
Protected virtual void Extract (PackagePart Document, string Folder)
{
Folder. ThrowIfNullOrEmpty ("Folder ");
String Location = Folder + System. Web. HttpUtility. UrlDecode (Document. Uri. ToString (). Replace ('\\','/');
New DirectoryInfo (Path. GetDirectoryName (Location). Create ();
Byte [] Data = new byte [1024];
Using (FileStream = new FileStream (Location, FileMode. Create ))
{
Stream DocumentStream = Document. GetStream ();
While (true)
{
Int Size = DocumentStream. Read (Data, zero, 1024 );
FileStream. Write (Data, 0, Size );
If (Size! = 1024)
Break;
}
FileStream. Close ();
}
}
/// <Summary>
/// Adds a file to the zip file
/// </Summary>
/// <Param name = "File"> File to add </param>
/// <Param name = "FileInfo"> File information </param>
/// <Param name = "Package"> Package to add the file to </param>
Protected virtual void AddFile (string File, FileInfo, Package)
{
File. ThrowIfNullOrEmpty ("File ");
If (! FileInfo. Exists)
Throw new ArgumentException ("FileInfo ");
Uri UriPath = PackUriHelper. CreatePartUri (new Uri (File, UriKind. Relative ));
PackagePart = Package. CreatePart (UriPath, System. Net. Mime. MediaTypeNames. Text. Xml, CompressionOption. Maximum );
Byte [] Data = FileInfo. ReadBinary ();
PackagePart. GetStream (). Write (Data, 0, Data. Count ());
Package. CreateRelationship (PackagePart. Uri, TargetMode. Internal, "http://schemas.microsoft.com/opc/2006/sample/document ");
}
# Endregion
# Region IDisposable Members
Public void Dispose ()
{
If (ZipFileStream! = Null)
{
ZipFileStream. Close ();
ZipFileStream. Dispose ();
ZipFileStream = null;
}
}
# Endregion
}
}
From weizhiai12's column