public Stream GetInputStream(uint index)
        {
            using (Stream stream = GetArchiveStream())
            {
                using (InStreamWrapper wrapper = new InStreamWrapper(stream))
                {
                    using (ArchiveOpenCallback callback = new ArchiveOpenCallback(this, ParentLayer.Parent, file))
                    {
                        ulong checkPos = 128 * 1024;
                        if (archive.Open(wrapper, ref checkPos, callback) != 0)
                        {
                            throw new FileSystemException(String.Format("Could not open archive file \"{0}\".", ParentLayer));
                        }

                        try
                        {
                            PropVariant sizeProperty = new PropVariant();
                            archive.GetProperty(index, ItemPropId.kpidSize, ref sizeProperty);

                            // TODO: Limit memory usage.

                            MemoryStream ms = new MemoryStream(sizeProperty.intValue);
                            archive.Extract(new[] {index}, 1, 0, new ArchiveExtractCallback(ms));
                            ms.Seek(0, SeekOrigin.Begin);

                            return ms;
                        }
                        finally
                        {
                            closeCommunicationLink();
                        }
                    }
                }
            }
        }
Example #2
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);
        }
Example #3
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);
                }
            }
        }
Example #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);
        }
Example #5
0
        public bool extract(string archiveName, uint fileNumber, string arcType, ProgressBar progress, Label status)
        {
            Program.form.waitForFreeMemory(status);
            attachedProgressBar = progress;
            attachedStatus      = status;
            status.Text         = "Extracting File " + this.extracting_file;
            progress.Maximum    = 1;
            progress.Value      = 0;
            Application.DoEvents();
            bool flag = false;
            KnownSevenZipFormat format = this.stringToSevenZipFormat(arcType);

            switch (format)
            {
            case KnownSevenZipFormat.SevenZip:
            case KnownSevenZipFormat.Zip:
                break;

            case KnownSevenZipFormat.Rar:
                return(this.extractRar(archiveName, progress, status, true));

            default:
                showError("1.0");
                break;
            }
            using (SevenZipFormat format2 = new SevenZipFormat(SevenZipDllPath))
            {
                if (this.ArchiveIn != null)
                {
                    Marshal.ReleaseComObject(this.ArchiveIn);
                    for (int i = 0; i < 5; i++)
                    {
                        Application.DoEvents();
                    }
                }
                this.ArchiveIn = format2.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(format));
                if (this.ArchiveIn == null)
                {
                    showError("1.1");
                    flag = false;
                }
                try
                {
                    using (InStreamWrapper wrapper = new InStreamWrapper(System.IO.File.OpenRead(archiveName)))
                    {
                        IArchiveOpenCallback openArchiveCallback = new ArchiveOpenCallback();
                        if (this.ArchiveIn.Open(wrapper, 0x20000L, openArchiveCallback) != 0)
                        {
                            showError("1.2");
                            flag = false;
                        }
                        else
                        {
                            PropVariant variant = new PropVariant();
                            this.ArchiveIn.GetProperty(fileNumber, ItemPropId.kpidPath, ref variant);
                            string fileName = (string)variant.GetObject();
                            this.ArchiveIn.Extract(new uint[] { fileNumber }, 1, 0, new ArchiveExtractCallback(fileNumber, fileName));
                            flag = true;
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(this.ArchiveIn);
                }
            }
            Program.form.waitForFreeMemory(status);
            return(flag);
        }
Example #6
0
        public int findFirstExtInArchive(string ext, string archiveName, string arcType, ProgressBar progress, Label status)
        {
            status.Text      = "Scanning Archive " + archiveName;
            progress.Maximum = 4;
            progress.Value   = 0;
            Application.DoEvents();
            int num = -1;

            using (SevenZipFormat format = new SevenZipFormat(SevenZipDllPath))
            {
                KnownSevenZipFormat sevenZip = KnownSevenZipFormat.SevenZip;
                arcType = arcType.ToLower();
                switch (arcType)
                {
                case "rar":
                    sevenZip = KnownSevenZipFormat.Rar;
                    break;

                case "zip":
                    sevenZip = KnownSevenZipFormat.Zip;
                    break;

                case "7z":
                    sevenZip = KnownSevenZipFormat.SevenZip;
                    break;

                default:
                    showError("0.1");
                    break;
                }
                IInArchive o = format.CreateInArchive(SevenZipFormat.GetClassIdFromKnownFormat(sevenZip));
                if (o == null)
                {
                    showError("0.2");
                    num = -1;
                }
                try
                {
                    using (InStreamWrapper wrapper = new InStreamWrapper(System.IO.File.OpenRead(archiveName)))
                    {
                        IArchiveOpenCallback openArchiveCallback = new ArchiveOpenCallback();
                        if (o.Open(wrapper, 0x40000L, openArchiveCallback) != 0)
                        {
                            showError("0.3");
                            num = -1;
                        }
                        uint numberOfItems = o.GetNumberOfItems();
                        fileInRar = "";
                        for (int i = 0; i < numberOfItems; i++)
                        {
                            PropVariant variant = new PropVariant();
                            o.GetProperty((uint)i, ItemPropId.kpidPath, ref variant);
                            PropVariant variant2 = new PropVariant();
                            o.GetProperty((uint)i, ItemPropId.kpidCRC, ref variant2);
                            if (Program.form.getFileExtension(variant.GetObject().ToString()).ToLower() == ext.ToLower())
                            {
                                num       = i;
                                fileInRar = variant.GetObject().ToString();
                                this.crc_of_extracting_file = long.Parse(variant2.GetObject().ToString()).ToString("X8");
                                return(num);
                            }
                        }
                        return(num);
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(o);
                }
            }
            return(num);
        }
        public override void init()
        {
            base.init();

            try
            {
                IList strongRef = new ArrayList(100);

                using(Stream stream = GetArchiveStream())
                {
                    using(InStreamWrapper wrapper = new InStreamWrapper(stream))
                    {
                        using (ArchiveOpenCallback callback = new ArchiveOpenCallback(this, ParentLayer.Parent, file))
                        {
                            ulong checkPos = 128 * 1024;
                            if (archive.Open(wrapper, ref checkPos, callback) != 0)
                            {
                                throw new FileSystemException(String.Format("Could not open Zip file \"{0}\".", ParentLayer));
                            }

                            uint count = archive.GetNumberOfItems();
                            for (uint index = 0; index < count; index++)
                            {
                                PropVariant entryName = new PropVariant();
                                archive.GetProperty(index, ItemPropId.kpidPath, ref entryName);

                                PropVariant hostSystem = new PropVariant();
                                archive.GetProperty(index, ItemPropId.kpidHostOS, ref hostSystem);

                                string path = (string) entryName.GetObject();
                                if (String.Compare((string) hostSystem.GetObject(), "FAT", 0) == 0)
                                {
                                    if (SevenZipFormat.SystemCodePage != SevenZipFormat.DefaultCodePage)
                                    {
                                        path = SevenZipFormat.DefaultEncoding.GetString(SevenZipFormat.SystemEncoding.GetBytes(path));
                                    }
                                }

                                IFileName name = FileSystemManager.resolveName(RootName, VirtualFileSystem.Provider.UriParser.encode(path));

                                PropVariant isFolderProperty = new PropVariant();
                                archive.GetProperty(index, ItemPropId.kpidIsFolder, ref isFolderProperty);

                                SevenZipFileObject fileObj;
                                if ((bool) isFolderProperty.GetObject() && getFileFromCache(name) != null)
                                {
                                    fileObj = (SevenZipFileObject) getFileFromCache(name);
                                    fileObj.Index = index;
                                    continue;
                                }

                                fileObj = createFileObject(name, index);
                                putFileToCache(fileObj);
                                strongRef.Add(fileObj);
                                fileObj.holdObject(strongRef);

                                SevenZipFileObject parent = null;
                                for (IFileName parentName = name.Parent; parentName != null; fileObj = parent, parentName = parentName.Parent)
                                {
                                    // Locate the parent
                                    parent = (SevenZipFileObject) getFileFromCache(parentName);
                                    if (parent == null)
                                    {
                                        parent = createFileObject(parentName, UInt32.MaxValue);

                                        putFileToCache(parent);
                                        strongRef.Add(parent);
                                        parent.holdObject(strongRef);
                                    }

                                    // Attach child to parent
                                    parent.attachChild(fileObj.Name);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                closeCommunicationLink();
            }
        }