Пример #1
0
        // Copies all source file into storage file
        private void Store(ref ZipEntry entry, Stream source)
        {
            uint   totalRead = 0;
            Stream outStream;

            long posStart    = this.zipFileStream.Position;
            long sourceStart = source.Position;

            outStream = this.zipFileStream;

            var inStream = new CrcStream(source);

            switch (entry.Method)
            {
            case Compression.LZMA:
                var encoder = new SevenZip.Compression.LZMA.Encoder();

                // LZMA "magic bytes" (7-zip requires them)
                var magicBytes = BitConverter.GetBytes(0x00051409);
                outStream.Write(magicBytes, 0, magicBytes.Length);

                encoder.WriteCoderProperties(outStream);

                encoder.Code(inStream, outStream, -1, -1, null);
                break;

            case Compression.Deflate:
                //using (var outPutStream = new ZOutputStream(outStream, zlibConst.Z_DEFAULT_COMPRESSION))
                var outPutStream = new Ionic.Zlib.DeflateStream(outStream, Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true);
                {
                    outPutStream.FlushMode = FlushType.Full;
                    int    buffSize = ZlibConstants.WorkingBufferSizeDefault;
                    byte[] buff     = new byte[buffSize];
                    int    size     = 0;
                    do
                    {
                        size = inStream.Read(buff, 0, buffSize);
                        if (size > 0)
                        {
                            outPutStream.Write(buff, 0, size);
                        }
                    } while (size > 0);
                }
                break;

            default:     //case Compression.Store:
                inStream.CopyTo(outStream);
                break;
            }
            entry.Crc32 = inStream.ReadCrc;
            outStream.Flush();

            entry.FileSize       = (uint)(source.Position - sourceStart);
            entry.CompressedSize = (uint)(this.zipFileStream.Position - posStart);

            // Verify for real compression
            if (entry.Method != Compression.Store && !this.ForceDeflating && source.CanSeek && entry.CompressedSize > entry.FileSize)
            {
                // Start operation again with Store algorithm
                entry.Method = Compression.Store;
                this.zipFileStream.Position = posStart;
                this.zipFileStream.SetLength(posStart);
                source.Position = sourceStart;
                this.Store(ref entry, source);
            }
        }
Пример #2
0
        /// <summary>
        /// Copy the contents of a stored file into an opened stream
        /// </summary>
        /// <param name="entry">Entry information of file to extract</param>
        /// <param name="_stream">Stream to store the uncompressed data</param>
        /// <returns>True if success, false if not.</returns>
        /// <remarks>Unique compression methods are Store and Deflate</remarks>
        public bool ExtractFile(ZipEntry entry, Stream stream)
        {
            if (!stream.CanWrite)
            {
                throw new InvalidOperationException("Stream cannot be written");
            }

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

            // Prepare streams
            stream.SetLength(entry.FileSize);
            var outStream = new CrcStream(stream);

            this.zipFileStream.Seek(entry.FileOffset, SeekOrigin.Begin);

            // Select input stream for inflating or just reading
            Stream inStream = new SegmentStream(this.zipFileStream, entry.CompressedSize);

            if (entry.Method == Compression.Deflate)
            {
                using (var intPutStream = new Ionic.Zlib.DeflateStream(inStream, Ionic.Zlib.CompressionMode.Decompress))
                {
                    intPutStream.FlushMode = FlushType.Full;
                    int    buffSize = 4096;
                    byte[] buff     = new byte[buffSize];
                    int    size     = 0;
                    do
                    {
                        size = intPutStream.Read(buff, 0, buffSize);
                        if (size > 0)
                        {
                            outStream.Write(buff, 0, size);
                        }
                    } while (size > 0);
                }
                //inStream = new DeflateStream(inStream, CompressionMode.Decompress, true);
            }

            // Decompress
            if (entry.Method == Compression.LZMA)
            {
                var decoder = new SevenZip.Compression.LZMA.Decoder();

                const int PropsLength = 5;
                var       buffer      = new byte[PropsLength];

                inStream.Read(buffer, 0, sizeof(int));
                if (BitConverter.ToInt32(buffer, 0) != 0x00051409)
                {
                    throw new Exception("Invalid LZMA stream signature");
                }

                if (inStream.Read(buffer, 0, PropsLength) != PropsLength)
                {
                    throw new Exception("Invalid LZMA properties length");
                }
                decoder.SetDecoderProperties(buffer);

                decoder.Code(inStream, outStream, entry.CompressedSize, entry.FileSize, null);
            }
            else
            {
                inStream.CopyTo(outStream);
            }

            stream.Flush();

            //if (entry.Method == Compression.Deflate)
            inStream.Dispose();
            if (entry.Crc32 != outStream.WriteCrc)
            {
                throw new Exception("Uncompressed file CRC mismatch");
            }
            return(true);
        }