C # compression and decompression (ZIP file)

xiaoxiao2021-03-06  17

C # compression and decompression (ZIP file) ---- ZipClass.cs

Using system.io; using iCsharpcode.sharpziplib.checksums; using icsharpcode.sharpziplib.zip; using icsharpcode.sharpziplib.gzip; namespace mywinzip {/////

/// Class1 summary description.

///

Public class zipclass

{

Public void zipfile (String Filetozip, String Zipedfile, Int compressionLevel, int blocksize)

{

// If the file is not found, it is wrong.

IF (! system.io.file.exists (filetozip))

{

Throw new system.io.filenotFoundException ("The Specified File" "Could Not Be found. zipping Aborderd");

}

System.IO.FileStream StreamTozip = New System.IO.FileStream (filetozip, system.io.filemode.open, system.io.fileAccess.read);

System.io.filestream zipfile = system.io.file.create (zipedfile);

ZipOutputStream ZipStream = New ZipOutputStream (zipfile);

ZIPENTRY ZIPENTRY = New Zipentry ("zippedfile");

Zipstream.putNextentry (ZIPENTRY);

ZipStream.Setlevel (CompressionLevel);

Byte [] buffer = new byte [blocksize];

System.Int32 size = streamtozip.read (buffer, 0, buffer.length);

ZipStream.write (buffer, 0, size);

Try

{

While (SIZE

{

INT SizeRead = streamtozip.read (buffer, 0, buffer.length);

ZipStream.write (buffer, 0, sizeread);

Size = sizeread;

}

}

Catch (System.exception EX)

{

Throw EX;

}

ZipStream.finish ();

Zipstream.close ();

Streamtozip.close ();

}

Public void zipfilemain (String [] ARGS)

{

String [] filenames = Directory.getFiles (args [0]);

CRC32 CRC = New CRC32 ();

ZipOutputStream S = New ZipOutputStream (file.create (args [1]));

S.Setlevel (6); // 0 - Store Only to 9 - Means Best Compression

FOREACH (String File in Filenames)

{

// Open the compressed file

FILESTREAM FS = file.openread (file);

Byte [] buffer = new byte [fs.length]; fs.read (buffer, 0, buffer.length);

ZIPENTRY Entry = new zipentry (file);

Entry.Datetime = datetime.now;

// set size and the crc, Because the information

// About the size and crc sales be stored in the header

// if it is not set it is automaticly written in the footer.

// (in this case size == CRC == -1 in the header)

// Some Zip Programs Have Problems With Zip Files That Don't Store

// The size and crc in the header.

Entry.size = fs.length;

fs.close ();

CRC.Reset ();

CRC.UPDATE (BUFFER);

Entry.crc = CRC.Value;

S.putNextentry (entry);

S.Write (buffer, 0, buffer.length);

}

S.finish ();

s.close ();

}

}

C # compression and decompression (ZIP file) ---- unzipClass.cs

Using system.collections; using system.io; using system.diagnostics; using system.runtime.serialization.formatters.binary; using system.data;

using ICSharpCode.SharpZipLib.BZip2; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using ICSharpCode.SharpZipLib.GZip; namespace MyWinzip {///

/// Class1 summary description.

///

Public class unzipClass

{

Public void unzip (String [] ARGS)

{

ZipinputStream S = New ZipinputStream (File.Openread (args [0]));

Zipentry.

While ((forentry = s.getnextentry ())! = NULL)

{

String DirectoryName = path.getdirectoryName (Args [1]);

String filename = path.getFileName (thents.name);

/ / Generate unzip catalog

Directory.created ;; DirectoryName;

IF (filename! = String.empty)

{

// Unzip file to the specified directory

String path = args [1] "

//" theentry.name;

FileStream Streamwriter = file.create (path);

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;

}

}

streamwriter.close ();

}

}

s.close ();

}

}

C # compression and decompression (ZIP file) ---- decompression application

New MyWinzip Engineering, reference Sharpziplib.dll, then add unzipClass.cs and zipclass.cs

Compiled into MyWinzip.dll to use.

Using mywinzip;

private void button1_Click (object sender, System.EventArgs e) {this.openFileDialog1.ShowDialog (); string filename = this.openFileDialog1.FileName; // file to be unpacked int i = filename.LastIndexOf ( "."); string filepath = filename.substring (0, i) "//"; // Decompressed target directory

String [] fileproperties = new string [2]; fileproperties [0] = fileproperties [1] = filepath; unzipclass unzc = new unzipClass (); unzc.unzip (fileproperties);

}

C # compression and decompression (ZIP file) ---- Web upload zip file, and decompress

Differently extract the * .dll file to the specified directory, read the * .txt file content (XML).

private void Button2_Click (object sender, System.EventArgs e) {try {string filename = upload.PostedFile.FileName; string unzippath // file to be decompressed = "c: / inetpub / wwwroot / maxservice / dll / handle /"; / / FileProperties = new string [] fileproperties = new string [] fileproperties = new string [] fileproperties = new string [] FileProperties = new string [] fileproperties = fileproperties [0] = filename; fileproperties [1] = unzippclass (); Unzc.unzip (FileProperties); if (unzc.xmlstring! = String.empty) {BOOL SUCCESS = this.Decodexml (unzc.xmlstring); // Subdeasing method, parse the TXT file content in the zip file (XML.) IF ( Success) this.label1.text = "Upload success."; else this.label1.text = "upload failed.";

}}} Catch (exception ex) {throw new exception;}}

转载请注明原文地址:https://www.9cbs.com/read-45189.html

New Post(0)