Ejemplo n.º 1
0
        private bool _ExtractTo( int index, Stream output, int outBufRemain )
        {
            XUnzipFileInfo fileInfo = _GetFileInfo ( index );

            BinaryReader br = new BinaryReader ( inputStream );

            if ( fileInfo.offsetData == -1 )
            {
                if ( inputStream.Seek ( fileInfo.offsetLocalHeader, SeekOrigin.Begin ) != fileInfo.offsetLocalHeader )
                    throw new Exception ( XUNZIP_ERR.CANT_READ_FILE.ToString () );
                if ( !ReadLocalHeader ( ref fileInfo, br ) )
                    return false;
            }

            inputStream.Seek ( fileInfo.offsetData, SeekOrigin.Begin );

            byte [] bufIn;
            int inputRemain;
            int outputRemain;
            int toRead;
            uint crc32 = 0;

            bufIn = new byte [ 32768 ];

            inputRemain = fileInfo.compressedSize;
            outputRemain = fileInfo.uncompressedSize;

            if ( fileInfo.method == XUNZIP_COMPRESSION_METHOD.METHOD_STORE )
            {
                CRC32 crcGen = new CRC32 ();
                while ( inputRemain > 0 )
                {
                    toRead = System.Math.Min ( System.Math.Min ( 32768, inputRemain ), outputRemain );

                    int temp = br.Read ( bufIn, 0, toRead );
                    output.Write ( bufIn, 0, toRead );
                    crcGen.Update ( bufIn, 0, toRead );
                    crc32 = crcGen.Result;

                    inputRemain -= toRead;
                    outputRemain -= temp;
                }
            }
            else
            {
                DeflateStream stream = new DeflateStream ( inputStream, CompressionMode.Decompress );

                CRC32 crcGen = new CRC32 ();
                while ( inputRemain > 0 )
                {
                    toRead = System.Math.Min ( System.Math.Min ( 32768, inputRemain ), outputRemain );

                    int temp = stream.Read ( bufIn, 0, outputRemain );
                    output.Write ( bufIn, 0, temp );
                    crcGen.Update ( bufIn, 0, toRead );
                    crc32 = crcGen.Result;

                    inputRemain -= toRead;
                    outputRemain -= temp;
                }
            }

            //if ( crc32 != fileInfo.crc32 )
            //return false;

            if ( outputRemain > 0 )
                return false;

            output.Position = 0;

            return true;
        }
Ejemplo n.º 2
0
        private void initStream()
        {
            if ( deflateStream != null ) return;
            CompressionLevel clevel = CompressionLevel.BestCompression;

            if ( compressLevel >= 1 && compressLevel <= 5 ) clevel = CompressionLevel.BestSpeed;
            else if ( compressLevel == 0 ) clevel = CompressionLevel.None;
            deflateStream = new DeflateStream ( rawStream, CompressionMode.Compress, clevel, true );
        }
Ejemplo n.º 3
0
        public static String UncompressString( byte [] compressed )
        {
            using ( var input = new System.IO.MemoryStream ( compressed ) )
            {
                Stream decompressor =
                    new DeflateStream ( input, CompressionMode.Decompress );

                return ZlibBaseStream.UncompressString ( compressed, decompressor );
            }
        }
Ejemplo n.º 4
0
 public static byte[] CompressString( string s )
 {
     using ( var ms = new MemoryStream () )
     {
         Stream compressor =
             new DeflateStream ( ms, CompressionMode.Compress, CompressionLevel.BestCompression );
         ZlibBaseStream.CompressString ( s, compressor );
         return ms.ToArray ();
     }
 }
Ejemplo n.º 5
0
        public static byte[] CompressBuffer( byte [] b )
        {
            using ( var ms = new System.IO.MemoryStream () )
            {
                Stream compressor =
                    new DeflateStream ( ms, CompressionMode.Compress, CompressionLevel.BestCompression );

                ZlibBaseStream.CompressBuffer ( b, compressor );
                return ms.ToArray ();
            }
        }
Ejemplo n.º 6
0
 private void initStream()
 {
     if ( deflateStream != null ) return;
     deflateStream = new DeflateStream ( rawStream, CompressionMode.Decompress, true );
 }