Recursive File compression

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. directoryservices;

Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. gzip;
Using icsharpcode. sharpziplib. checksums;

Public class ziphelper
{

.........

# Region compressed folder, supporting Recursion

/// <Summary>
/// Compressed folder
/// </Summary>
/// <Param name = "dir"> folder to be compressed </param>
/// <Param name = "targetfilename"> compressed file path (including file name) </param>
/// <Param name = "recursive"> recursive compression </param>
/// <Returns> </returns>
Public static bool compress (string Dir, string targetfilename, bool recursive)
{
// If the target file already exists, check whether the file is overwritten.
If (file. exists (targetfilename ))
{
If (! _ Processoverwrite (targetfilename ))
Return false;
}

If (recursive = false)
Return compress (Dir, targetfilename );


Filestream zipfile;
Zipoutputstream zipstream;

// Open
Zipfile = file. Create (targetfilename );
Zipstream = new zipoutputstream (zipfile );

If (Dir! = String. Empty)
{
_ Compressfolder (Dir, zipstream, DIR );
}

// Close
Zipstream. Finish ();
Zipstream. Close ();

If (file. exists (targetfilename ))
Return true;
Else
Return false;
}

/// <Summary>
/// Compress a subfolder
/// </Summary>
/// <Param name = "basepath"> </param>
/// <Param name = "zips"> </param>
/// <Param name = "zipfolername"> </param>
Private Static void _ compressfolder (string basepath, zipoutputstream zips, string zipfolername)
{
If (file. exists (basepath ))
{
_ AddFile (basepath, zips, zipfolername );
Return;
}
String [] names = directory. getfiles (basepath );
Foreach (string filename in names)
{
_ AddFile (filename, zips, zipfolername );
}

Names = directory. getdirectories (basepath );
Foreach (string Foldername in names)
{
_ Compressfolder (Foldername, zips, zipfolername );
}

}

/// <Summary>
/// Compress a subfile
/// </Summary>
/// <Param name = "FILENAME"> </param>
/// <Param name = "zips"> </param>
/// <Param name = "zipfolername"> </param>
Private Static void _ AddFile (string filename, zipoutputstream zips, string zipfolername)
{
If (file. exists (filename ))
{
_ Createzipfile (filename, zips, zipfolername );
}
}

/// <Summary>
/// Compress a single file
/// </Summary>
/// <Param name = "filetozip"> </param>
/// <Param name = "zips"> </param>
/// <Param name = "zipfolername"> </param>
Private Static void _ createzipfile (string filetozip, zipoutputstream zips, string zipfolername)
{
Try
{
Filestream streamtozip = new filestream (filetozip, filemode. Open, fileaccess. Read );
String temp = filetozip;
String temp1 = zipfolername;
If (temp1.length> 0)
{
Int I = temp1.lastindexof ('\') + 1;
Int J = temp. Length-I;
Temp = temp. substring (I, j );
}
Zipentry zipen = new zipentry (temp );

Zips. putnextentry (zipen );
Byte [] buffer = new byte [16384];
System. int32 size = streamtozip. Read (buffer, 0, buffer. Length );
Zips. Write (buffer, 0, size );
Try
{
While (size <streamtozip. length)
{
Int sizeread = streamtozip. Read (buffer, 0, buffer. Length );
Zips. Write (buffer, 0, sizeread );
Size + = sizeread;
}
}
Catch (system. Exception ex)
{
Throw ex;
}

Streamtozip. Close ();
}
Catch (exception E)
{
Throw E;
}
}
# 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.