public Byte[] Extract(CentralDirectoryFileHeader header)
        {
            var local = new LocalFileHeader(header.Bytes, header.RelativeOffsetOfLocalFileHeader);
            var pos   = local.Offset + local.Length;

            var dst = new Byte[local.UncompressedSize];

#if true
            using (var s = new MemoryStream(header.Bytes, pos, local.CompressedSize, false))
                using (var deflateStream = new DeflateStream(s, CompressionMode.Decompress))
                {
                    int dst_pos = 0;
                    for (int remain = dst.Length; remain > 0;)
                    {
                        var readSize = deflateStream.Read(dst, dst_pos, remain);
                        dst_pos += readSize;
                        remain  -= readSize;
                    }
                }
#else
            var size = RawInflate.RawInflateImport.RawInflate(dst, 0, dst.Length,
                                                              header.Bytes, pos, header.CompressedSize);
#endif

            return(dst);
        }
예제 #2
0
        public Byte[] Extract(CentralDirectoryFileHeader header)
        {
            var local            = new LocalFileHeader(header.Bytes, header.RelativeOffsetOfLocalFileHeader);
            var compressedSize   = local.CompressedSize;
            var uncompressedSize = local.UncompressedSize;

            var pos = local.Offset + local.Length;

            if (header.HasDataDescriptor)
            {
                var descriptor = new DataDescriptor(header.Bytes, pos + header.CompressedSize);
                compressedSize   = descriptor.CompressedSize;
                uncompressedSize = descriptor.UncompressedSize;
            }

            if (compressedSize != header.CompressedSize)
            {
                throw new Exception();
            }
            if (uncompressedSize != header.UncompressedSize)
            {
                throw new Exception();
            }

            var dst = new Byte[uncompressedSize];

            switch (header.CompressionMethod)
            {
            case CompressionMethod.Stored:
            {
                Buffer.BlockCopy(header.Bytes, pos, dst, 0, compressedSize);
            }
            break;

            case CompressionMethod.Deflated:
            {
                using (var s = new MemoryStream(header.Bytes, pos, compressedSize, false))
                    using (var deflateStream = new DeflateStream(s, CompressionMode.Decompress))
                    {
                        int dst_pos = 0;
                        for (int remain = dst.Length; remain > 0;)
                        {
                            var readSize = deflateStream.Read(dst, dst_pos, remain);
                            dst_pos += readSize;
                            remain  -= readSize;
                        }
                    }
            }
            break;

            default:
                throw new NotImplementedException();
            }

            return(dst);
        }
        public string ExtractToString(CentralDirectoryFileHeader header, Encoding encoding)
        {
            var local = new LocalFileHeader(header.Bytes, header.RelativeOffsetOfLocalFileHeader);
            var pos   = local.Offset + local.Length;

            using (var s = new MemoryStream(header.Bytes, pos, local.CompressedSize, false))
                using (var deflateStream = new DeflateStream(s, CompressionMode.Decompress))
                    using (var r = new StreamReader(deflateStream, encoding))
                    {
                        return(r.ReadToEnd());
                    }
        }
        public static ZipArchiveStorage Parse(byte[] bytes)
        {
            var eocd    = EOCD.Parse(bytes);
            var archive = new ZipArchiveStorage();

            var pos = eocd.OffsetOfStartOfCentralDirectory;

            for (int i = 0; i < eocd.NumberOfCentralDirectoryRecordsOnThisDisk; ++i)
            {
                var file = new CentralDirectoryFileHeader(bytes, pos);
                archive.Entries.Add(file);
                pos += file.Length;
            }

            return(archive);
        }