Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /////////////////////////////////////////////////////////////////
        // Extract file
        /////////////////////////////////////////////////////////////////

        private Boolean ExtractFile()
        {
            // translate to file header
            FileHeader FH = ZipDir[DirIndex];

            // test skip read only
            if (ProgramState.State.SkipReadOnly && (FH.FileAttr & FileAttributes.ReadOnly) != 0)
            {
                AppendStatus("Skip read only");
                return(true);
            }

            // test skip hidden file
            if (ProgramState.State.SkipHidden && (FH.FileAttr & FileAttributes.Hidden) != 0)
            {
                AppendStatus("Skip hidden file");
                return(true);
            }

            // test skip system file
            if (ProgramState.State.SkipSystem && (FH.FileAttr & FileAttributes.System) != 0)
            {
                AppendStatus("Skip system file");
                return(true);
            }

            // file name
            String FileName = (FH.Path && ProgramState.State.IgnoreFolderInfo) ?
                              FH.FileName.Substring(FH.FileName.LastIndexOf('\\') + 1) : FH.FileName;

            // full ourput file name
            String OutputFile = ProgramState.State.ExtractToFolder + "\\" + FileName;

            // path part of file name
            Int32 Ptr = OutputFile.LastIndexOf('\\');

            // we have a path, make sure path exists
            if (Ptr >= 0)
            {
                // skip older files
                if (ProgramState.State.SkipOlder)
                {
                    // exiting file
                    DateTime ExistingFileTime = File.GetLastWriteTime(OutputFile);

                    // compressed file
                    // convert dos file date and time to DateTime format
                    DateTime CompFileTime = new DateTime(1980 + ((FH.FileDate >> 9) & 0x7f), (FH.FileDate >> 5) & 0xf, FH.FileDate & 0x1f,
                                                         (FH.FileTime >> 11) & 0x1f, (FH.FileTime >> 5) & 0x3f, 2 * (FH.FileTime & 0x1f));

                    // compare times
                    if (CompFileTime < ExistingFileTime)
                    {
                        AppendStatus("Skip too old");
                        return(true);
                    }
                }

                // make sure directory exists
                if (!Directory.Exists(OutputFile.Substring(0, Ptr)))
                {
                    // make new folder
                    try
                    {
                        Directory.CreateDirectory(OutputFile.Substring(0, Ptr));
                    }
                    catch
                    {
                        AppendStatus("Path Error");
                        return(true);
                    }
                }

                // directory
                if ((FH.FileAttr & FileAttributes.Directory) != 0)
                {
                    AppendStatus("Dir-OK");
                    return(false);
                }
            }

            // test if file exists
            if (File.Exists(OutputFile))
            {
                // no overwrite
                if (ProgramState.State.Overwrite == (Int32)OverwriteFiles.No)
                {
                    AppendStatus("No overwrite");
                    return(true);
                }

                // ask overwrite permission
                if (ProgramState.State.Overwrite == (Int32)OverwriteFiles.Ask)
                {
                    if (MessageBox.Show(this, "Do you want to overwrite: " + OutputFile + " ?\n(Press Cancel to abort extraction)", "Overwrite warning",
                                        MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    {
                        AppendStatus("No overwrite");
                        return(true);
                    }
                }

                // check for read only file
                if (!ProgramState.State.SkipReadOnly)
                {
                    FileAttributes Attr = File.GetAttributes(OutputFile);
                    if ((Attr & FileAttributes.ReadOnly) != 0)
                    {
                        try
                        {
                            File.SetAttributes(OutputFile, Attr & ~FileAttributes.ReadOnly);
                        }
                        catch {}
                    }
                }

                // delete file
                try
                {
                    File.Delete(OutputFile);
                }
                catch
                {
                    AppendStatus("Overwrite failed");
                    return(true);
                }
            }

            // decompress file
            if (Inflate.DecompressZipFile(FH, null, OutputFile, true, true))
            {
                Trace.Write("Decompression Error\n" + Inflate.ExceptionStack[0] + "\n" + Inflate.ExceptionStack[1]);
                AppendStatus("Decompression failed [" + Deflate.ExceptionStack[0] + "]");
                return(true);
            }

            // successful return
            AppendStatus("File-OK");
            return(false);
        }