//////////////////////////////////////////////////////////////////// // Open zip file //////////////////////////////////////////////////////////////////// private void OnOpen ( object sender, EventArgs e ) { // get zip file name from operator OpenFileDialog OFD = new OpenFileDialog(); OFD.Filter = "Zip files (*.ZIP)|*.ZIP"; OFD.CheckFileExists = true; OFD.CheckPathExists = true; OFD.RestoreDirectory = true; if (OFD.ShowDialog() != DialogResult.OK) { return; } // display empty datagrid AddDataGrid(); // trace Trace.Write("Open exising ZIP archive: " + OFD.FileName); // create inflate object if (Inflate == null) { Inflate = new InflateZipFile(); } // inflate is already open else if (InflateZipFile.IsOpen(Inflate)) { // delete zip directory ZipDir = null; // close previous zip archive (if open) Inflate.CloseZipFile(); // display empty directory in data grid view LoadDataGrid(); } // open zip file and load zip file directory if (Inflate.OpenZipFile(OFD.FileName)) { MessageBox.Show("Open ZIP file Error\n" + Inflate.ExceptionStack[0] + "\n" + Inflate.ExceptionStack[1]); return; } // get zip directory ZipDir = Inflate.ZipDir; // display zip file directory in data grid view LoadDataGrid(); return; }
//////////////////////////////////////////////////////////////////// // Form is closing //////////////////////////////////////////////////////////////////// private void OnClosing ( object sender, FormClosingEventArgs e ) { // Inflate is active. Close the zip archive file if (InflateZipFile.IsOpen(Inflate)) { Inflate.CloseZipFile(); } // deflate is active if (Deflate != null) { // new archive is not empty if (!Deflate.IsEmpty) { switch (MessageBox.Show(this, "Do you want to save your ZIP archive?\n" + "If you select NO, the archive will be deleted", "Save ZIP Archive", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)) { case DialogResult.Cancel: e.Cancel = true; return; case DialogResult.Yes: OnSave(); break; case DialogResult.No: // clearing the zip directory will make SaveArchive delete the file Deflate.ClearArchive(); break; } } // deflate is empty else { // save archive will delete the archive because it is empty Deflate.ClearArchive(); } } // trace exit Trace.Write("UZipDotNet exit"); return; }
//////////////////////////////////////////////////////////////////// // Switch from extract zip to update zip //////////////////////////////////////////////////////////////////// private Boolean SwitchMode() { // inflate archive is not open if (!InflateZipFile.IsOpen(Inflate)) { return(false); } // ask operator ArchiveUpdateForm Dialog = new ArchiveUpdateForm(); Dialog.Inflate = Inflate; if (Dialog.ShowDialog(this) == DialogResult.Cancel) { return(true); } // create deflate object if (Deflate == null) { Deflate = new DeflateZipFile(); } // reopen exiting zip file for read and write if (Deflate.OpenArchive(Inflate.ArchiveName)) { MessageBox.Show("Reopen ZIP file Error\n" + Deflate.ExceptionStack[0] + "\n" + Deflate.ExceptionStack[1]); // get link to the empty zip directory ZipDir = null; // display zip file directory in data grid view LoadDataGrid(); return(true); } // get zip directory ZipDir = Deflate.ZipDir; // display zip file directory in data grid view LoadDataGrid(); // successful switch return(false); }
//////////////////////////////////////////////////////////////////// // Display archive name //////////////////////////////////////////////////////////////////// private void DisplayArchiveName() { // archive is open for reading if (InflateZipFile.IsOpen(Inflate)) { ArchiveNameLabel.BackColor = Color.FromArgb(192, 255, 255); ArchiveNameLabel.Text = Inflate.ArchiveName; } // archive is open for writing else if (DeflateZipFile.IsOpen(Deflate)) { ArchiveNameLabel.BackColor = Color.FromArgb(255, 192, 255); ArchiveNameLabel.Text = Deflate.ArchiveName; } // archive is not open else { ArchiveNameLabel.BackColor = SystemColors.Control; ArchiveNameLabel.Text = "UZipDotNet"; } // calculate required width Int32 Width = (Int32)Math.Ceiling(CreateGraphics().MeasureString(ArchiveNameLabel.Text, ArchiveNameLabel.Font).Width) + 50; // archive name position ArchiveNameLabel.Width = Math.Min(Math.Max(Width, 200), ClientSize.Width - 50); ArchiveNameLabel.Left = (ClientSize.Width - ArchiveNameLabel.Width) / 2; ArchiveNameLabel.Top = 8; // enable/disable some buttons OpenButton.Enabled = !DeflateZipFile.IsOpen(Deflate); ExtractButton.Enabled = InflateZipFile.IsOpen(Inflate); AddButton.Enabled = InflateZipFile.IsOpen(Inflate) || DeflateZipFile.IsOpen(Deflate); DeleteButton.Enabled = AddButton.Enabled; // change button name NewButton.Text = DeflateZipFile.IsOpen(Deflate) ? "Save" : "New"; // update control Refresh(); return; }
//////////////////////////////////////////////////////////////////// // 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; }
public static Boolean IsOpen(InflateZipFile Inflate) { return(Inflate != null && Inflate.ReadFile != null); }
//////////////////////////////////////////////////////////////////// // 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); }
public static bool IsOpen(InflateZipFile inflate) { return inflate != null && inflate._readFile != null; }
public static bool IsOpen(InflateZipFile inflate) { return(inflate != null && inflate._readFile != null); }