Ejemplo n.º 1
0
        public List <MemoryStream> ExtractZipToStream(string ZipFullPath, string SearchPattern, string Password)
        {
            List <MemoryStream> aZipStream = new List <MemoryStream>();

            ZipFile zf = null;

            try
            {
                FileStream fs = File.OpenRead(ZipFullPath);
                zf = new ZipFile(fs);
                if (!string.IsNullOrEmpty(Password))
                {
                    zf.Password = Password;                             // AES encrypted entries are handled automatically
                }

                long TotalCount = zf.Count;

                foreach (ZipEntry Entry in zf)
                {
                    if (!Entry.IsFile)
                    {
                        continue;                                               // Ignore directories
                    }
                    if (!CLang.Like(Entry.Name, SearchPattern, true))
                    {
                        continue;
                    }

                    string DirectoryName = Path.GetDirectoryName(Entry.Name);
                    string FileName      = Path.GetFileName(Entry.Name);

                    byte[]       buffer    = new byte[4096];                    // 4K is optimum
                    Stream       zipStream = zf.GetInputStream(Entry);
                    MemoryStream ms        = CFile.GetMemoryStreamFromStream(zipStream);
                    aZipStream.Add(ms);
                }
            }
            finally
            {
                if (zf != null)
                {
                    zf.IsStreamOwner = true;        // Makes close also shut the underlying stream
                    zf.Close();                     // Ensure we release resources
                }
            }

            return(aZipStream);
        }