Пример #1
0
        void loadDirectory()
        {
            var maxOffset = directoryOffset + directorySize;

            streamPosition = directoryOffset;
            int recordIndex = 0;

            fileHeaders.Clear();
            fileDict.Clear();
            while (streamPosition < maxOffset)
            {
                var curRec = new CentralDirectoryFileHeader();
                curRec.loadFixedPart(reader);
                if (!curRec.hasValidSignature())
                {
                    throw new System.ArgumentException(
                              string.Format("Invalid signature in record {0}, file \"{1}\"", recordIndex, archiveFileName));
                }
                if ((curRec.getVariablePartSize() + streamPosition) > maxOffset)
                {
                    throw new System.ArgumentException(
                              string.Format("Record {0} too big in file \"{1}\"", recordIndex, archiveFileName)
                              );
                }
                curRec.loadVariablePart(reader);

                //Debug.LogFormat("Record {0}: {1}", recordIndex, curRec);

                fileHeaders.Add(curRec);
                fileDict.Add(convertToDictFileName(curRec.filename), recordIndex);
                recordIndex++;
            }
        }
Пример #2
0
        public byte[] decompressFile(CentralDirectoryFileHeader inHeader)
        {
            streamPosition = inHeader.fileHeaderOffset;
            var localHeader = new LocalHeader();

            localHeader.load(reader);

            var compressedBytes = reader.ReadBytes((int)inHeader.compressedSize);

            if (inHeader.compressionMethod == 0)
            {
                return(compressedBytes);
            }

            if (inHeader.compressionMethod != 8)             //deflate
            {
                throw new System.ArgumentException(
                          string.Format("Unsupported compression method {0} in file \"{1}\" of archive \"{2}\"",
                                        inHeader.filename, inHeader.compressionMethod, archiveFileName));
            }

            var tmp = new byte[65536];

            using (var memStream = new System.IO.MemoryStream(compressedBytes))
                using (var deflateStream = new System.IO.Compression.DeflateStream(memStream, System.IO.Compression.CompressionMode.Decompress))
                    using (var outStream = new System.IO.MemoryStream()){
                        int bytesRead;
                        while ((bytesRead = deflateStream.Read(tmp, 0, tmp.Length)) > 0)
                        {
                            outStream.Write(tmp, 0, bytesRead);
                        }
                        return(outStream.ToArray());
                    }
        }