System.IO.Compression.ZipStorer.ExtractFile C# (CSharp) Méthode

ExtractFile() public méthode

Copy the contents of a stored file into an opened stream
Unique compression methods are Store and Deflate
public ExtractFile ( ZipFileEntry _zfe, System.Stream _stream ) : bool
_zfe ZipFileEntry Entry information of file to extract
_stream System.Stream Stream to store the uncompressed data
Résultat bool
        public bool ExtractFile(ZipFileEntry _zfe, Stream _stream)
        {
            if (!_stream.CanWrite)
                throw new InvalidOperationException("Stream cannot be written");

            // check signature
            byte[] signature = new byte[4];
            this.ZipFileStream.Seek(_zfe.HeaderOffset, SeekOrigin.Begin);
            this.ZipFileStream.Read(signature, 0, 4);
            if (BitConverter.ToUInt32(signature, 0) != 0x04034b50)
                return false;

            // Select input stream for inflating or just reading
            Stream inStream;
            if (_zfe.Method == Compression.Store)
                inStream = this.ZipFileStream;
            else if (_zfe.Method == Compression.Deflate)
                inStream = new DeflateStream(this.ZipFileStream, CompressionMode.Decompress, true);
            else
                return false;

            // Buffered copy
            byte[] buffer = new byte[16384];
            this.ZipFileStream.Seek(_zfe.FileOffset, SeekOrigin.Begin);
            uint bytesPending = _zfe.FileSize;
            uint crc32 = 0 ^ 0xffffffff;
            while (bytesPending > 0)
            {
                int bytesRead = inStream.Read(buffer, 0, (int)Math.Min(bytesPending, buffer.Length));
                _stream.Write(buffer, 0, bytesRead);
                for (uint i = 0; i < bytesRead; i++)
                {
                    crc32 = ZipStorer.CrcTable[(crc32 ^ buffer[i]) & 0xFF] ^ (crc32 >> 8);
                }
                bytesPending -= (uint)bytesRead;
            }
            _stream.Flush();
            crc32 ^= 0xffffffff;

            if (_zfe.Method == Compression.Deflate)
                inStream.Dispose();
            return crc32 == _zfe.Crc32;
        }

Same methods

ZipStorer::ExtractFile ( ZipFileEntry _zfe, string _filename ) : bool

Usage Example

Exemple #1
0
        /// <summary>
        ///     Removes one of many files in storage. It creates a new Zip file.
        /// </summary>
        /// <param name="zip">Reference to the current Zip object</param>
        /// <param name="zfes">List of Entries to remove from storage</param>
        /// <returns>True if success, false if not</returns>
        /// <remarks>This method only works for storage of type FileStream</remarks>
        public static bool RemoveEntries(ref ZipStorer zip, List <ZipFileEntry> zfes)
        {
            if (!(zip.zipFileStream is FileStream))
            {
                throw new InvalidOperationException("RemoveEntries is allowed just over streams of type FileStream");
            }


            //Get full list of entries
            List <ZipFileEntry> fullList = zip.ReadCentralDir();

            //In order to delete we need to create a copy of the zip file excluding the selected items
            string tempZipName   = Path.GetTempFileName();
            string tempEntryName = Path.GetTempFileName();

            try
            {
                ZipStorer tempZip = Create(tempZipName, string.Empty);

                foreach (ZipFileEntry zfe in fullList)
                {
                    if (!zfes.Contains(zfe))
                    {
                        if (zip.ExtractFile(zfe, tempEntryName))
                        {
                            tempZip.AddFile(zfe.Method, tempEntryName, zfe.FilenameInZip, zfe.Comment);
                        }
                    }
                }
                zip.Close();
                tempZip.Close();

                if (File.Exists(zip.fileName))
                {
                    File.Replace(tempZipName, zip.fileName, null, true);
                }
                else
                {
                    File.Move(tempZipName, zip.fileName);
                }

                zip = Open(zip.fileName, zip.access);
            }
            catch
            {
                return(false);
            }
            finally
            {
                if (File.Exists(tempZipName))
                {
                    File.Delete(tempZipName);
                }
                if (File.Exists(tempEntryName))
                {
                    File.Delete(tempEntryName);
                }
            }
            return(true);
        }
All Usage Examples Of System.IO.Compression.ZipStorer::ExtractFile