Пример #1
0
        ////////////////////////////////////////////////////////////////////
        // 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;
        }
Пример #2
0
        private void OnClosing
        (
            object sender,
            FormClosingEventArgs e
        )
        {
            // user pressed cancel
            if (DialogResult == DialogResult.Cancel)
            {
                return;
            }

            // user does not want to save the file
            if (NoSaveRadioButton.Checked)
            {
                // close zip file
                Inflate.CloseZipFile();
                return;
            }

            // create backup copy name
            Int32  Ptr = Inflate.ArchiveName.LastIndexOf('.');
            String BackupName;

            for (Int32 No = 0;; No++)
            {
                String CopyMsg = No == 0 ? " - Copy" : String.Format(" - Copy{0}", No);
                BackupName = Inflate.ArchiveName.Insert(Ptr, CopyMsg);
                if (!File.Exists(BackupName))
                {
                    break;
                }
            }

            try
            {
                // make a copy of the open archive file
                File.Copy(Inflate.ArchiveName, BackupName);
            }
            catch (Exception Ex)
            {
                // copy failed
                MessageBox.Show(this, "Backup copy failed\n" + Ex.Message,
                                "Backup Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Cancel = true;
                return;
            }

            // close zip file
            Inflate.CloseZipFile();

            // save to recycle bin
            if (RecycleBinRadioButton.Checked)
            {
                try
                {
                    // save file to recycle bin
                    MSFileIO.FileSystem.DeleteFile(Inflate.ArchiveName, MSFileIO.UIOption.OnlyErrorDialogs, MSFileIO.RecycleOption.SendToRecycleBin);
                }
                catch (Exception Ex)
                {
                    // save file to recycle bin failed
                    MessageBox.Show(this, "Send to recycle bin failed\n" +
                                    "File saved as: " + BackupName + "\n" + Ex.Message,
                                    "Send to Recycle Bin Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                try
                {
                    // rename the backup copy to original name
                    File.Move(BackupName, Inflate.ArchiveName);
                }
                catch (Exception Ex)
                {
                    // rename failed
                    MessageBox.Show(this, "Rename failed\n" + Ex.Message,
                                    "Rename Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    throw new ApplicationException("Archive file is not available\n" + Inflate.ArchiveName);
                }
            }

            // successful return with DialogResult.OK
            return;
        }
Пример #3
0
        ////////////////////////////////////////////////////////////////////
        // 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);
        }