Exemple #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);
        }
Exemple #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);
                }
            }
        }
        private static void ListOrExtract(string archiveName, uint level)
        {
            using (SevenZipFormat Format = new SevenZipFormat(SevenZipDllPath))
            {
            IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(KnownSevenZipFormat.Zip));
            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)
                        //ShowHelp();
                    if (!Directory.Exists(@"tmp_dir"))
                        Directory.CreateDirectory(@"tmp_dir");

                    //Console.WriteLine(archiveName);

                    uint Count = Archive.GetNumberOfItems();
                    for (uint I = 0; I < Count; I++)
                    {
                        PropVariant Name = new PropVariant();
                        Archive.GetProperty(I, ItemPropId.kpidPath, ref Name);
                        string FileName = (string)Name.GetObject();
                        Program.f1.label1.Text = FileName;
                        Application.DoEvents();
                        for(int i = 0; i < level; i++)
                            Console.Write("\t");
                        Console.Write(FileName + "\n");
                        FileName += level;
                        Archive.Extract(new uint[] { I }, 1, 0, new ArchiveExtractCallback(I, FileName));
                        Program.Determine_Parser(new System.IO.FileInfo("tmp_dir//" + FileName), level);
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(Archive);
                Directory.Delete(@"tmp_dir", true);
            }
            }
        }
Exemple #4
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);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
              {
            ShowHelp();
            return;
              }

              try
              {
            string ArchiveName;
            uint FileNumber = 0xFFFFFFFF;
            bool Extract;

            switch (args[0])
            {
              case "l":
            ArchiveName = args[1];
            Extract = false;
            break;
              case "e":
            ArchiveName = args[1];
            Extract = true;
            if ((args.Length < 3) || !uint.TryParse(args[2], out FileNumber))
            {
              ShowHelp();
              return;
            }
            break;
              default:
            ShowHelp();
            return;
            }

            using (SevenZipFormat Format = new SevenZipFormat(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "7z.dll")))
            {
              IInArchive Archive = Format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(KnownSevenZipFormat.Zip));
              if (Archive == null)
              {
            ShowHelp();
            return;
              }

              try
              {
            using (InStreamWrapper ArchiveStream = new InStreamWrapper(File.OpenRead(ArchiveName)))
            {
              ulong CheckPos = 32 * 1024;
              if (Archive.Open(ArchiveStream, ref CheckPos, null) != 0)
                ShowHelp();

              Console.Write("Archive: ");
              Console.WriteLine(ArchiveName);

              if (Extract)
              {
                PropVariant Name = new PropVariant();
                Archive.GetProperty(FileNumber, ItemPropId.kpidPath, ref Name);
                string FileName = (string)Name.GetObject();

                Console.Write("Extracting: ");
                Console.Write(FileName);
                Console.Write(' ');

                Archive.Extract(new uint[] { FileNumber }, 1, 0, new ArchiveCallback(FileNumber, FileName));
              }
              else
              {
                Console.WriteLine("List:");
                uint Count = Archive.GetNumberOfItems();
                for (uint I = 0; I < Count; I++)
                {
                  PropVariant Name = new PropVariant();
                  Archive.GetProperty(I, ItemPropId.kpidPath, ref Name);
                  Console.Write(I);
                  Console.Write(' ');
                  Console.WriteLine(Name.GetObject());
                }
              }
            }
              }
              finally
              {
            Marshal.ReleaseComObject(Archive);
              }
            }
              }
              catch (Exception e)
              {
            Console.Write("Error: ");
            Console.WriteLine(e.Message);
              }
        }
        public int GetStream(string name, out IInStream inStream)
        {
            if (_subArchiveMode)
            {
                inStream = null;
                return HRESULT.S_FALSE;
            }

            if (_streams.ContainsKey(name))
            {
                inStream = _streams[name];
            }
            else
            {
                string path = Path.Combine(Path.GetDirectoryName(_fileName), name);
                try
                {
                    InStreamWrapper stream = new InStreamWrapper(File.OpenRead(path));
                    _volumes.Add(path);
                    _streams.Add(path, stream);
                    _currentVolumeSize = stream.BaseStream.Length;
                    _packedSize += _currentVolumeSize;
                    inStream = stream;
                }
                catch (FileNotFoundException)
                {
                    inStream = null;
                    return HRESULT.S_FALSE;
                }
                catch (Exception e)
                {
                    if (_exception == null)
                    {
                        _exception = new SevenZipException("Unable to open the volume: " + path, e);
                    }
                    inStream = null;
                    return HRESULT.E_INVALIDARG;
                }
            }
            return HRESULT.S_OK;
        }