Пример #1
0
        public string Extract(string archiveName, string folderToExtract, uint fileNumber, KnownSevenZipFormat ZipFormat, out bool isDirectory)
        {
            isDirectory = false;

            ZipFormatG = ZipFormat;

            string FileName = null;

            try
            {
                using (SevenZipFormat Format = new SevenZipFormat(SevenZipDllPath))
                {
                    IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(ZipFormat));
                    if (Archive == null)
                    {
                        return(null);
                    }

                    try
                    {
                        using (InStreamWrapper ArchiveStream = new InStreamWrapper(File.OpenRead(archiveName)))
                        {
                            IArchiveOpenCallback OpenCallback = new ArchiveOpenCallback();

                            ulong CheckPos = 256 * 1024;

                            if (Archive.Open(ArchiveStream, ref CheckPos, OpenCallback) != 0)
                            {
                                return(null);
                            }

                            PropVariant Name = new PropVariant();
                            Archive.GetProperty(fileNumber, ItemPropId.kpidPath, ref Name);
                            FileName = (string)Name.GetObject();
                            Archive.GetProperty(fileNumber, ItemPropId.kpidIsFolder, ref Name);
                            isDirectory = (bool)Name.GetObject();

                            if (!isDirectory)
                            {
                                Archive.Extract(new uint[] { fileNumber }, 1, 0, new ArchiveExtractCallback(fileNumber, folderToExtract + FileName));
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(Archive);
                    }
                }
            }
            catch
            {
            }
            return(FileName);
        }
Пример #2
0
        private static void ListOrExtract(string archiveName, string extractLocation, bool extract)
        {
            using (SevenZipFormat Format = new SevenZipFormat(SevenZDllPath)) {
                IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(KnownSevenZipFormat.SevenZip));
                if (Archive == null)
                {
                    return;
                }

                try {
                    using (InStreamWrapper ArchiveStream = new InStreamWrapper(File.OpenRead(archiveName))) {
                        IArchiveOpenCallback OpenCallback = new ArchiveOpenCallback();

                        // 32k CheckPos is not enough for some 7z archive formats
                        ulong CheckPos = 128 * 1024;
                        if (Archive.Open(ArchiveStream, ref CheckPos, OpenCallback) != 0)
                        {
                            return;
                        }

                        if (extract)
                        {
                            uint Count = Archive.GetNumberOfItems();
                            for (int i = 0; i < Count; i++)
                            {
                                PropVariant Name = new PropVariant();
                                Archive.GetProperty((uint)i, ItemPropId.kpidPath, ref Name);
                                string FileName = (string)Name.GetObject();
                                Archive.Extract(new uint[] { (uint)i }, 1, 0, new ArchiveExtractCallback((uint)i, FileName, extractLocation));
                            }
                        }
                        else
                        {
                            //Console.WriteLine("List:");
                            String files = "";
                            uint   Count = Archive.GetNumberOfItems();
                            for (uint I = 0; I < Count; I++)
                            {
                                PropVariant Name = new PropVariant();
                                Archive.GetProperty(I, ItemPropId.kpidPath, ref Name);
                                files += String.Format("{0} - {1}\r\n", I, Name.GetObject());
                            }
                            MessageBox.Show(files);
                        }
                    }
                } finally {
                    Marshal.ReleaseComObject(Archive);
                }
            }
        }
Пример #3
0
        public List <string> List(string archiveName, KnownSevenZipFormat ZipFormat)
        {
            List <string> List = new List <string>();

            using (SevenZipFormat Format = new SevenZipFormat(SevenZipDllPath))
            {
                IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(ZipFormat));
                if (Archive == null)
                {
                    return(List);
                }

                try
                {
                    using (InStreamWrapper ArchiveStream = new InStreamWrapper(File.OpenRead(archiveName)))
                    {
                        IArchiveOpenCallback OpenCallback = new ArchiveOpenCallback();

                        ulong CheckPos = 256 * 1024;

                        if (Archive.Open(ArchiveStream, ref CheckPos, OpenCallback) != 0)
                        {
                            return(List);
                        }

                        uint Count = Archive.GetNumberOfItems();
                        for (uint I = 0; I < Count; I++)
                        {
                            PropVariant Name = new PropVariant();
                            Archive.GetProperty(I, ItemPropId.kpidPath, ref Name);
                            string[] T = Name.GetObject().ToString().Split('\\');
                            List.Add(T[T.Length - 1]);
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(Archive);
                }
            }

            return(List);
        }
Пример #4
0
 private string ExtractFile(string fileName)
 {
     if (isMono)
         return fileName;
     SystemState old = state;
     state = SystemState.SystemPause;
     string ext = Path.GetExtension(fileName).ToLower();
     if (ext == ".7z" || ext == ".zip" || ext == ".rar")
     {
         string sevenZ = this.config["7z"];
         if (IntPtr.Size == 8)
             sevenZ = this.config["7z64"]; //replace this with installer logic maybe?
         SevenZipFormat Format = new SevenZipFormat(sevenZ);
         KnownSevenZipFormat fileType;
         switch (ext)
         {
             default:
             case ".7z":
                 fileType = KnownSevenZipFormat.SevenZip;
                 break;
             case ".zip":
                 fileType = KnownSevenZipFormat.Zip;
                 break;
             case ".rar":
                 fileType = KnownSevenZipFormat.Rar;
                 break;
         }
         IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(fileType));
         try
         {
             InStreamWrapper ArchiveStream = new InStreamWrapper(File.OpenRead(fileName));
             ulong checkPos = 32 * 1024;
             Archive.Open(ArchiveStream, ref checkPos, null);
             uint count = Archive.GetNumberOfItems();
             string[] archiveContent = new string[count];
             for (uint i = 0; i < count; i++)
             {
                 PropVariant name = new PropVariant();
                 Archive.GetProperty(i, ItemPropId.kpidPath, ref name);
                 archiveContent[i] = name.GetObject().ToString();
             }
             if (count == 1)
             {
                 fileName = Path.Combine(this.config["tmpDir"], Path.GetFileNameWithoutExtension(archiveContent[0]) + ".tmp" + Path.GetExtension(archiveContent[0]));
                 Archive.Extract(new uint[] { 0 }, 1, 0, new ArchiveCallback(0, fileName));
             }
             else
             {
                 ArchiveViewer viewer = new ArchiveViewer(archiveContent);
                 if (viewer.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                 {
                     fileName = Path.Combine(this.config["tmpDir"], Path.GetFileNameWithoutExtension(archiveContent[viewer.selectedFile]) + ".tmp" + Path.GetExtension(archiveContent[0]));
                     Archive.Extract(new uint[] { (uint)viewer.selectedFile }, 1, 0, new ArchiveCallback((uint)viewer.selectedFile, fileName));
                 }
                 else
                 {
                     fileName = "";
                 }
             }
             ArchiveStream.Dispose();
         }
         finally
         {
             Archive.Close();
             Marshal.ReleaseComObject(Archive);
         }
         Format.Dispose();
     }
     state = old;
     return fileName;
 }