C # third-party zip decompression and compression tool with case source

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using ICSharpCode.SharpZipLib.Zip; Open source Tools, free to download::

Http://files.cnblogs.com/xiaowei0705/SharpZipLib_0860_Bin.zip
Using System.IO;

Namespace Package
{
Class Class1
{
#region Compression Decompression method
<summary>
Function: Compress files (temporarily compress only the files in the folder level directory, and the folders and their children are ignored)
</summary>
<param name= "Dirpath" > Compressed folder path </param>
<param name= "Zipfilepath" > Generate compressed file path, empty default to the same level directory as the compressed folder, the name is: folder name +.zip</param>
<param name= "Err" > Error message </param>
<returns> is compression successful </returns>
public bool ZipFile (string Dirpath, String Zipfilepath, out string err)
{
Err = "";
if (Dirpath = = string. Empty)
{
Err = "The folder you want to compress cannot be empty!" ";
return false;
}
if (! Directory.Exists (Dirpath))
{
Err = "The folder to be compressed does not exist!" ";
return false;
}
Use folder name when compressed file name is empty +.zip
if (Zipfilepath = = string. Empty)
{
if (dirpath.endswith ("\ \"))
{
Dirpath = dirpath.substring (0, dirpath.length-1);
}
Zipfilepath = Dirpath + ". zip";
}

Try
{
string[] filenames = Directory.GetFiles (Dirpath);
using (zipoutputstream s = new Zipoutputstream (File.create (Zipfilepath)))
{
S.setlevel (9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry (path.getfilename (file));
Entry. DateTime = DateTime.Now;
S.putnextentry (entry);
using (FileStream fs = File.openread (File))
{
int sourcebytes;
Do
{
Sourcebytes = fs. Read (buffer, 0, buffer. Length);
S.write (buffer, 0, sourcebytes);
} while (Sourcebytes > 0);
}
}
S.finish ();
S.close ();
}
}
catch (Exception ex)
{
Err = ex. Message;
return false;
}
return true;
}

<summary>
Function: Unzip files in zip format.
</summary>
<param name= "Zipfilepath" > Compressed file path </param>
<param name= "Unzipdir" > Unzip the file storage path, default to the same directory as the compressed file when empty, folder with the same name as the compressed file </param>
<param name= "Err" > Error message </param>
<returns> whether the decompression is successful </returns>
public bool Unzipfile (string Zipfilepath, String unzipdir, out string err)
{
Err = "";
if (Zipfilepath = = string. Empty)
{
Err = "Compressed file cannot be empty!" ";
return false;
}
if (! File.exists (Zipfilepath))
{
Err = "The compressed file does not exist!" ";
return false;
}
The folder with the same name as the compressed file is in the same directory as the compressed file when the Unpacked folder is empty
if (Unzipdir = = string. Empty)
Unzipdir = Zipfilepath.replace (Path.getfilename (Zipfilepath), Path.getfilenamewithoutextension (ZipFilePath));
if (!unzipdir.endswith ("\ \"))
Unzipdir + = "\";
if (! Directory.Exists (Unzipdir))
Directory.CreateDirectory (Unzipdir);

Try
{
using (zipinputstream s = new Zipinputstream (File.openread (Zipfilepath)))
{

ZipEntry Theentry;
while ((Theentry = S.getnextentry ()) = null)
{
String directoryname = Path.getdirectoryname (theentry.name);
String fileName = Path.getfilename (theentry.name);
if (Directoryname.length > 0)
{
Directory.CreateDirectory (Unzipdir + directoryname);
}
if (!directoryname.endswith ("\ \"))
DirectoryName + = "\";
if (fileName! = String.Empty)
{
using (FileStream StreamWriter = file.create (Unzipdir + theentry.name))
{

int size = 2048;
byte[] data = new byte[2048];
while (true)
{
Size = s.read (data, 0, data. Length);
if (Size > 0)
{
StreamWriter.Write (data, 0, size);
}
Else
{
Break
}
}
}
}
}//while
}
}
catch (Exception ex)
{
Err = ex. Message;
return false;
}
return true;
}//Decompression End
#endregion


}
}

Reproduced in original: http://www.cnblogs.com/xiaowei0705/archive/2011/05/16/2047828.html

C # third-party zip decompression and compression tool with case source

Related Article

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.