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);
        }
Exemplo n.º 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());
                    }
        }
Exemplo n.º 4
0
        public ArraySegment <byte> Get(string url)
        {
            var found = Entries.FirstOrDefault(x => x.FileName == url);

            if (found == null)
            {
                throw new FileNotFoundException("[ZipArchive]" + url);
            }

            switch (found.CompressionMethod)
            {
            case CompressionMethod.Deflated:
                return(new ArraySegment <byte>(Extract(found)));

            case CompressionMethod.Stored:
                var local = new LocalFileHeader(found.Bytes, found.RelativeOffsetOfLocalFileHeader);
                var pos   = local.Offset + local.Length;
                return(new ArraySegment <byte>(local.Bytes, pos, local.CompressedSize));
            }

            throw new NotImplementedException(found.CompressionMethod.ToString());
        }