所属类别:.NET
文章作者:佚名
特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!
//ZipWriter.csusing System;using System.Runtime.Serialization;namespace OrganicBit.Zip {/// Provides support for writing files in the ZIP file format. Includes support for both compressed and uncompressed entries./// This example shows how to create a ZIP file./// /// public static void Add(string zipFileName, string[] entryPatterns) {///string currentDirectory = Directory.GetCurrentDirectory();///Console.WriteLine("Creating {0}", zipFileName);//////ZipWriter writer = new ZipWriter(zipFileName);//////// buffer to hold temp bytes///byte[] buffer = new byte[4096];///int byteCount;//////// add files to archive///foreach (string pattern in entryPatterns) {///foreach (string path in Directory.GetFiles(currentDirectory, pattern)) {///string fileName = Path.GetFileName(path);///Console.Write("Adding {0}", fileName);//////ZipEntry entry = new ZipEntry(fileName);///entry.ModifiedTime = File.GetLastWriteTime(fileName);///entry.Comment = "local file comment";//////writer.AddEntry(entry);//////FileStream reader = File.OpenRead(entry.Name);///while ((byteCount = reader.Read(buffer, 0, buffer.Length)) > 0) {///Console.Write(".");///writer.Write(buffer, 0, byteCount);///}///reader.Close();///Console.WriteLine();///}///}//////writer.Close();/// }/// /// public class ZipWriter : IDisposable {/// Name of the zip file.string _fileName;/// Zip file global comment.string _comment = "";/// True if currently writing a new zip file entry.bool _entryOpen = false;/// Zip file handle.IntPtr _handle = IntPtr.Zero;/// Initializes a new instance fo the class with a specified file name.Any Existing file will be overwritten./// The name of the zip file to create.public ZipWriter(string fileName) {_fileName = fileName;_handle = ZipLib.zipOpen(fileName, 0);if (_handle == IntPtr.Zero) {string msg = String.Format("Could not open zip file '{0}' for writing.", fileName);throw new ZipException(msg);}}/// Cleans up the resources used by this zip file.~ZipWriter() {CloseFile();}/// Dispose is synonym for Close.void IDisposable.Dispose() {Close();}/// Closes the zip file and releases any resources.public void Close() {// Free unmanaged resources.CloseFile();// If base type implements IDisposable we would call it here.// Request the system not call the finalizer method for this object.GC.SuppressFinalize(this);}/// Gets the name of the zip file.public string Name {get {return _fileName;}}/// Gets and sets the zip file comment.public string Comment {get { return _comment; }set { _comment = value; }}/// Creates a new zip entry in the directory and positions the stream to the start of the entry data./// The zip entry to be written./// Closes the current entry if still active.public void AddEntry(ZipEntry entry) {ZipFileEntryInfo info;info.DateTime = entry.ModifiedTime;int result;unsafe {byte[] extra = null;uint extraLength = 0;if (entry.ExtraField != null) {extra = entry.ExtraField;extraLength = (uint) entry.ExtraField.Length;}result = ZipLib.zipOpenNewFileInZip(_handle,entry.Name,&info,extra,extraLength,null, 0,entry.Comment,(int) entry.Method,entry.Level);}_entryOpen = true;}/// Compress a block of bytes from the given buffer and writes them into the current zip entry./// The array to read data from./// The byte offset in at which to begin reading./// The maximum number of bytes to write.public void Write(byte[] buffer, int index, int count) {int result = ZipLib.zipWriteInFileInZip(_handle, buffer, (uint) count);}private void CloseEntry() {if (_entryOpen) {int result = ZipLib.zipCloseFileInZip(_handle);_entryOpen = false;}}void CloseFile() {if (_handle != IntPtr.Zero) {CloseEntry();int result = ZipLib.zipClose(_handle, _comment);if (result < 0) {throw new ZipException("Could not close zip file.");}_handle = IntPtr.Zero;}}}} 关闭本页
相关信息· 单元测试应该测什么,不应该测什么?
· 如何查看unix下某个oracle OS的进程在做什么操作
· 为了安全高速 装完XP系统必做六件大事
· 在ORACLE里设置访问多个SQL Server数据库
35534
79329
