コード例 #1
0
 private static bool CompressionBenchmark(Stream inStream, Stream outStream,
                                          OutArchiveFormat format, CompressionMethod method,
                                          ref LibraryFeature?features, LibraryFeature testedFeature)
 {
     try
     {
         var compr = new SevenZipCompressor {
             ArchiveFormat = format, CompressionMethod = method
         };
         compr.CompressStream(inStream, outStream);
     }
     catch (Exception)
     {
         return(false);
     }
     features |= testedFeature;
     return(true);
 }
コード例 #2
0
        private static bool ExtractionBenchmark(string archiveFileName, Stream outStream, ref LibraryFeature?features, LibraryFeature testedFeature)
        {
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceString(archiveFileName));

            try
            {
                using (var extractor = new SevenZipExtractor(stream))
                {
                    extractor.ExtractFile(0, outStream);
                }
            }
            catch (Exception)
            {
                return(false);
            }

            features |= testedFeature;
            return(true);
        }
コード例 #3
0
        private static bool TryLoad7ZipLibrary(string dll)
        {
            try
            {
                if (File.Exists(dll))
                {
                    SevenZipBase.SetLibraryPath(dll);
                    LibraryFeature features = SevenZipBase.CurrentLibraryFeatures;

                    return(features.HasFlag(LibraryFeature.Compress7z) && features.HasFlag(LibraryFeature.Extract7z));
                }
                else
                {
                    //MessageBox.Show("7zip library not found:\r\n" + dll, "Load error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
            }
            catch // (Exception x)
            {
                //MessageBox.Show("Couldn't load 7zip library Exception:\r\n" + x.Message+"\r\n"+dll, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }
コード例 #4
0
        private void CompressForm_Shown(object sender, EventArgs e)
        {
            switch (task)
            {
            case ZipTaskType.doZip:
                Text = "Add to archive";
                break;

            case ZipTaskType.doUnZip:
                Text = "Extract from archive";
                break;

            default:
                Text         = "Unknown command";
                DialogResult = DialogResult.Abort;
                return;
            }
            // Start here
            // Toggle between the x86 and x64 bit dll
            if ((SevenZipDLLPath == null) || (SevenZipDLLPath == string.Empty))
            {
                SevenZipDLLPath = TryGet7ZipLibrary();
            }

            if (SevenZipDLLPath == string.Empty)
            {
                DialogResult = DialogResult.Abort;
                return;
            }
            LibraryFeature features = SevenZipBase.CurrentLibraryFeatures;

            lZipFile.Text = ArchiveFileName;

            switch (task)
            {
            case ZipTaskType.doZip:

                if (!features.HasFlag(LibraryFeature.Compress7z))
                {
                    MessageBox.Show("Currently loaded 7-zip library doesn't support 7z compression, cannot continue !\r\n" + SevenZipDLLPath, "Method not supported", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    DialogResult = DialogResult.Abort;
                    return;
                }

                if (File.Exists(ArchiveFileName))
                {
                    if (MessageBox.Show("Target 7z file already exists, do you want to overwrite it ?\r\n" + ArchiveFileName, "Overwrite File", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    {
                        DialogResult = DialogResult.Cancel;
                        return;
                    }
                }

                try
                {
                    zipper = new SevenZipCompressor();
                    zipper.CompressionLevel  = SevenZip.CompressionLevel.Ultra;
                    zipper.CompressionMethod = CompressionMethod.Lzma;
                    zipper.CompressionMode   = CompressionMode.Create;
                    zipper.TempFolderPath    = Path.GetTempPath();
                    zipper.ArchiveFormat     = OutArchiveFormat.SevenZip;
                    bgwZipper.RunWorkerAsync();
                }
                catch (Exception x)
                {
                    MessageBox.Show("Exception:\r\n" + x.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.Abort;
                }
                break;

            case ZipTaskType.doUnZip:
                if (!File.Exists(ArchiveFileName))
                {
                    MessageBox.Show("Source 7z file not found ?\r\n" + ArchiveFileName, "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    DialogResult = DialogResult.Cancel;
                    return;
                }

                if ((Path.GetExtension(ArchiveFileName).ToLower() == ".7z") && (!features.HasFlag(LibraryFeature.Extract7z)))
                {
                    MessageBox.Show("Currently loaded 7-zip library doesn't support .7z extraction, cannot continue !\r\n" + SevenZipDLLPath, "Method not supported", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    DialogResult = DialogResult.Abort;
                    return;
                }

                if ((Path.GetExtension(ArchiveFileName).ToLower() == ".zip") && (!features.HasFlag(LibraryFeature.ExtractZip)))
                {
                    MessageBox.Show("Currently loaded 7-zip library doesn't support .zip extraction, cannot continue !\r\n" + SevenZipDLLPath, "Method not supported", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    DialogResult = DialogResult.Abort;
                    return;
                }

                if ((Path.GetExtension(ArchiveFileName).ToLower() == ".rar") && (!features.HasFlag(LibraryFeature.ExtractRar)))
                {
                    MessageBox.Show("Currently loaded 7-zip library doesn't support .rar extraction, cannot continue !\r\n" + SevenZipDLLPath, "Method not supported", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    DialogResult = DialogResult.Abort;
                    return;
                }

                try
                {
                    if (overrideZipFormat)
                    {
                        unzipper = new SevenZipExtractor(ArchiveFileName, InArchiveFormat.SevenZip);
                    }
                    else
                    {
                        unzipper = new SevenZipExtractor(ArchiveFileName);
                    }

                    if (unzipper.FilesCount <= 0)
                    {
                        MessageBox.Show("Nothing to extract", "No files", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        DialogResult = DialogResult.Cancel;
                    }
                    else
                    {
                        pb.Minimum = 0;
                        pb.Maximum = (int)unzipper.FilesCount + 1;
                        pb.Step    = 1;
                        pb.Value   = 0;
                        // unzipper.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(FileExtractStarted);
                        unzipper.FileExtractionFinished += new EventHandler <FileInfoEventArgs>(FileExtractFinished);
                        unzipper.FileExists             += new EventHandler <FileOverwriteEventArgs>(FileExtractFileExists);
                        bgwUnZipper.RunWorkerAsync();
                    }
                }
                catch (Exception x)
                {
                    MessageBox.Show("Exception:\r\n" + x.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.Abort;
                }
                break;

            default:
                DialogResult = DialogResult.Abort;
                return;
            }
        }
コード例 #5
0
 private static bool ExtractionBenchmark(string archiveFileName, Stream outStream,
     ref LibraryFeature? features, LibraryFeature testedFeature)
 {
     var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
             GetResourceString(archiveFileName));
     try
     {
         using (var extr = new SevenZipExtractor(stream))
         {
             extr.ExtractFile(0, outStream);
         }
     }
     catch (Exception)
     {
         return false;
     }
     features |= testedFeature;
     return true;
 }
コード例 #6
0
 private static bool CompressionBenchmark(Stream inStream, Stream outStream,
     OutArchiveFormat format, CompressionMethod method,
     ref LibraryFeature? features, LibraryFeature testedFeature)
 {
     try
     {
         var compr = new SevenZipCompressor { ArchiveFormat = format, CompressionMethod = method };
         compr.CompressStream(inStream, outStream);
     }
     catch (Exception)
     {
         return false;
     }
     features |= testedFeature;
     return true;
 }