Zipfile of the compressed file class

Source: Internet
Author: User
Tags getstream
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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.