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
}
}