|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace ZipTest
{
class ZipHelp
{
/// <summary>
/// Zip a file
/// </summary>
/// <param name="SrcFile">source file path</param>
/// <param name="DstFile">zipped file path</param>
/// <param name="BufferSize">buffer to use</param>
public static void Zip(string SrcFile, string DstFile, int BufferSize)
{
FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
FileStream fileStreamOut = new FileStream(DstFile, FileMode.Create, FileAccess.Write);
ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
byte[] buffer = new byte[BufferSize];
ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
zipOutStream.PutNextEntry(entry);
int size;
do
{
size = fileStreamIn.Read(buffer, 0, buffer.Length);
zipOutStream.Write(buffer, 0, size);
} while (size > 0);
zipOutStream.Close();
fileStreamOut.Close();
fileStreamIn.Close();
}
/// <summary>
/// UnZip a file
/// </summary>
/// <param name="SrcFile">source file path</param>
/// <param name="DstFile">unzipped file path</param>
/// <param name="BufferSize">buffer to use</param>
public static void UnZip(string SrcFile, string DstFile, int BufferSize)
{
FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
ZipEntry entry = zipInStream.GetNextEntry();
FileStream fileStreamOut = new FileStream(DstFile + @"\" + entry.Name, FileMode.Create, FileAccess.Write);
int size;
byte[] buffer = new byte[BufferSize];
do
{
size = zipInStream.Read(buffer, 0, buffer.Length);
fileStreamOut.Write(buffer, 0, size);
} while (size > 0);
zipInStream.Close();
fileStreamOut.Close();
fileStreamIn.Close();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I live in Xi'an(西安). It has 3000+ years history and is one of the birthplaces of the ancient civilization of China.
accelerate your life.