// File dialog box OpenFileDialog OFD = new new OpenFileDialog (); // File Filter ofd.Filter = " text document (* .txt) | * .txt | * * | * *.. " ; // whether to click OK button iF (ofd.ShowDialog () == DialogResult.OK) { String fileName = ofd.FileName; // obtain selected file name (full path) textBox1.Text = fileName; } // compression // determines whether the selected to unzip the file IF ( String .IsNullOrEmpty (textBox1.Text.Trim ())) { MessageBox.Show ( "You must select the compressed file " ); return ; } // determine whether a file exists IF (! ) File.Exists (textBox1.Text.Trim ()) { MessageBox.Show ( " You selected compressed file does not exist " ); return ; } // Get the directory where the file to be compressed, this directory may be re-generated using the compressed packet String fileBasePath = Path.GetDirectoryName (textBox1.Text.Trim ()); // Get removing the file to be compressed suffix file name after name string fileNameWithoutExtension = Path.GetFileNameWithoutExtension (textBox1.Text.Trim ()); // ZIP compressed file name string+ = fileNameWithoutExtension zipFileName " .zip " ; // save the address zip file, that full path String zipFilrePath = Path.Combine (fileBasePath, zipFileName); // create a compressed package (equivalent to create a project) a using (the ZipFile the ZipFile = ZipFile.Create (zipFilrePath)) { // starts updating archive (corresponding to open the box) zipfile.BeginUpdate (); // add a file to the compressed package zipfile.Add (textBox1.Text.Trim (), Path.GetFileName (textBox1.Text.Trim ())); // submit updated zipfile.CommitUpdate (); } MessageBox.Show ( " completion of compression " ); // selected compressed archive // instantiate a file selection dialog box the OpenFileDialog the OFD = new new the OpenFileDialog (); // File Filter ofd.Filter = " compressed file (*. ZIP) | * .zip " ; IF (ofd.ShowDialog () == DialogResult.OK) { String fileName = ofd.FileName; // obtain selected file name (full path) TextBox2.Text = fileName; } // select Extract directory the FolderBrowserDialog FBD = new newThe FolderBrowserDialog (); IF (fbd.ShowDialog () == DialogResult.OK) { String selectedPath = fbd.SelectedPath; // Get selected folder path textBox3.Text = selectedPath; } // start extracting String zipFileName = TextBox2.Text; String = descFilePath textBox3.Text; BOOL RET = the this .Decompress (zipFileName, descFilePath); IF (RET) { MessageBox.Show ( " decompression success " ); } the else { MessageBox.Show ( " decompression failed " ); }
/// <Summary> /// decompression /// </ Summary> /// <param name = "a sourceFile"> compressed full path address </ param> /// <param name = "The targetPath"> codecs where the path is </ param> /// <Returns> </ Returns> public BOOL Decompress ( String a sourceFile, String The targetPath) { // determine whether the source file exists iF (! the File.Exists (a sourceFile)) { the throw new new a FileNotFoundException ( String .Format ( " Can not find file '{0}'", sourceFile)); } // Analyzing the target folder exists IF (! Directory.Exists (The targetPath)) { Directory.CreateDirectory (The targetPath); } // Create a ZipInput stream, and read the compressed file need to unpack the using ( var ZipInputStream = new new ZipInputStream ( File.OpenRead (a sourceFile))) { // definition of a compressed item entry the ZipEntry theEntry; the while ((theEntry = ZipInputStream.getNextEntry ()) =! null ) { // Get directory entry item after compression a decompressed file: Extract directory entry in the directory + String directorName =Path.Combine (The targetPath, Path.GetDirectoryName (theEntry.Name)); // determines whether the extracted directory exists, does not exist as the new over- IF (! Directory.Exists (directorName)) { Directory.CreateDirectory (directorName); } / / decide whether an entry is a folder, the folder if it is directly next cycle IF (theEntry.IsDirectory) { Continue ; } // can be performed here, a file description entry is // Get a compressed file entry item after extracting the full path: directory + filename String fileName = Path.Combine (directorName, Path.GetFileName (theEntry.Name)); IF(! String.IsNullOrEmpty (fileName)) { // create a file stream, an entry for the file entry is written to the new file in the using (= the FileStream streamWriter File.Create (fileName)) { int size = . 4 * 1024 ; // set the buffer size, that is, each up to a maximum number byte byte [] buffer = new new byte [size]; // set a buffer size = zipInputStream.Read (buffer, 0 , buffer.Length); // ZipInput read from the stream read, returns the read byte length, the while (size> 0 ) // cycle is determined, if the size is greater than 0 indicates read content { StreamWriter.Write (Buffer, 0 ;, size) // (extract from the file) read the contents of the write target file size = zipInputStream.Read (Buffer, 0 , buffer.Length); // continue to flow from ZipInput reading the contents read, returns the read byte length } } } } } return to true ; }