. Net compressed file stream

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. IO. Compression;
Using System. IO;
 
Namespace gzipfile
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
 
Private void button#click (object sender, EventArgs e)
{

 
 
// File stream
FileStream reader;
Reader = File. Open ("D: \ ddd.txt", FileMode. Open );
FileStream writer;
Writer = File. Create ("D: \ ddd.gz ");
 
// Compress related streams
MemoryStream MS = new MemoryStream ();
GZipStream zipStream = new GZipStream (MS, CompressionMode. Compress, true );
 
// Write data to the compressed stream
Byte [] sourceBuffer = new byte [reader. Length];
 
 
Reader. Read (sourceBuffer, 0, sourceBuffer. Length );
ZipStream. Write (sourceBuffer, 0, sourceBuffer. Length );
 
// Make sure to disable the compressed stream before the memory stream is read.
ZipStream. Close ();
ZipStream. Dispose ();
 
// Read data from the memory stream
Ms. Position = 0; // Note: Do not omit this sentence
Byte [] destBuffer = new byte [ms. Length];
// Ms. Read (destBuffer, 0, destBuffer. Length );
 
Byte [] header = new byte [10];
Ms. Read (header, 0, 10 );
Header [3] = 8; // indicates the file name information.
Byte [] fielContent = new byte [ms. Length-10];
Ms. Read (fielContent, 0, fielContent. Length );
 
String strfilename = "widebright's file .txt"; // specify the initial file name

// Byte [] filename = System. Text. Encoding. Convert (System. Text. Encoding. Default,
/// System. Text. Encoding. GetEncoding ("ISO-8859-1 "),
// System. Text. Encoding. Default,
// System. Text. Encoding. Default. GetBytes (strfilename ));
Byte [] filename = System. Text. Encoding. Default. GetBytes (strfilename );
 
Writer. Write (header, 0, header. Length );
Writer. Write (filename, 0, filename. Length );
Writer. WriteByte (0); // the file name ends in 0 bytes.
Writer. Write (fielContent, 0, fielContent. Length );
 
// Close and release the memory stream
Ms. Close ();
Ms. Dispose ();
 
// Close and release the file stream
Writer. Close ();
Writer. Dispose ();
Reader. Close ();
Reader. Dispose ();
 
 
}
}
}

From _ ya

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.