//////////////////////////////////////////////////////////////////// // New/Save archive file //////////////////////////////////////////////////////////////////// private void OnNew ( object sender, EventArgs e ) { // test for save mode if (DeflateZipFile.IsOpen(Deflate)) { OnSave(); return; } // get zip file name from operator SaveFileDialog SFD = new SaveFileDialog(); SFD.Filter = "Zip files (*.zip)|*.zip"; SFD.CheckFileExists = false; SFD.CheckPathExists = true; SFD.RestoreDirectory = true; SFD.OverwritePrompt = true; SFD.DefaultExt = "zip"; SFD.AddExtension = true; if (SFD.ShowDialog() != DialogResult.OK) { return; } // display empty datagrid AddDataGrid(); // trace Trace.Write("Create ZIP archive: " + SFD.FileName); // Inflate file is open if (InflateZipFile.IsOpen(Inflate)) { // release zip directory object ZipDir = null; // close inflate zip file if open Inflate.CloseZipFile(); // display zip file directory in data grid view LoadDataGrid(); } // create deflate object if (Deflate == null) { Deflate = new DeflateZipFile(); } // create empty zip file if (Deflate.CreateArchive(SFD.FileName)) { MessageBox.Show("Create ZIP file Error\n" + Deflate.ExceptionStack[0] + "\n" + Deflate.ExceptionStack[1]); return; } // get link to the empty zip directory ZipDir = Deflate.ZipDir; // display zip file directory in data grid view LoadDataGrid(); return; }
//////////////////////////////////////////////////////////////////// // Test Single File //////////////////////////////////////////////////////////////////// private Int32 TestZip() { // add extension to compressed file name CompFileName += ".zip"; // create compressiom object DeflateZipFile Def = new DeflateZipFile(CompLevel); // create empty zip file if (Def.CreateArchive(CompFileName)) { // save exception stack on error ExceptionStack = Def.ExceptionStack; return(1); } // split the input file name to path and name String FileName; Int32 Ptr = InputFileName.LastIndexOf('\\'); FileName = Ptr < 0 ? InputFileName : InputFileName.Substring(Ptr + 1); // save start time Int32 StartTime = Environment.TickCount; // compress file if (Def.Compress(InputFileName, FileName)) { // save exception stack on error ExceptionStack = Def.ExceptionStack; return(1); } // save compression elapse time CompTime = Environment.TickCount - StartTime; // save input and compressed file length InputFileLen = Def.ReadTotal; CompFileLen = Def.WriteTotal; // save archive if (Def.SaveArchive()) { // save exception stack on error ExceptionStack = Def.ExceptionStack; return(1); } // save start time StartTime = Environment.TickCount; // create decompression object InflateZipFile Inf = new InflateZipFile(); // open the zip file if (Inf.OpenZipFile(CompFileName)) { // save exception stack on error ExceptionStack = Inf.ExceptionStack; return(2); } // decompress the file if (Inf.DecompressZipFile(Inf.ZipDir[0], null, DecompFileName, false, true)) { // save exception stack on error ExceptionStack = Inf.ExceptionStack; return(2); } // save decompression elapse time DecompTime = Environment.TickCount - StartTime; // save restored file length DecompFileLen = Inf.WriteTotal; // close the zip file Inf.CloseZipFile(); // successful exit return(0); }