Exemplo n.º 1
0
        private static void ReadZipFileComment(ZipFile zf)
        {
            // no seek - stream integrity should be protected by the caller

            byte[] block = new byte[2];
            zf.ReadStream.Read(block, 0, block.Length);
            short commentLength = (short)(block[0] + (block[1] * 0x100));

            if (commentLength > 0)
            {
                block = new byte[commentLength];
                zf.ReadStream.Read(block, 0, block.Length);
                if (ZipSharedUtilities.HighBytes(block) && (zf._encoding == Encoding.GetEncoding("ibm437")))
                {
                    zf._encoding = Encoding.UTF8;
                }
                zf.Comment = ZipSharedUtilities.StringFromBuffer(block, block.Length, zf._encoding);
            }
        }
Exemplo n.º 2
0
        private void WriteHeader(Stream s, int cycle)
        {
            byte[] bytes = new byte[0x2200];
            int    i     = 0;

            bytes[i++] = 80;
            bytes[i++] = 0x4b;
            bytes[i++] = 3;
            bytes[i++] = 4;
            short VersionNeededToExctract = 20;

            bytes[i++] = (byte)(VersionNeededToExctract & 0xff);
            bytes[i++] = (byte)((VersionNeededToExctract & 0xff00) >> 8);
            byte[] FileNameBytes  = this.GetFileNameBytes();
            short  filenameLength = (short)FileNameBytes.Length;

            this._CommentBytes = null;
            if (!String.IsNullOrEmpty(this._Comment))
            {
                this._CommentBytes = this._Comment.ToByteArray(this._encoding);
            }
            bool setUtf8Bit = this.UseUtf8Encoding && (ZipSharedUtilities.HighBytes(this._CommentBytes) || ZipSharedUtilities.HighBytes(FileNameBytes));

            this._BitField = ((short)0);
            if (setUtf8Bit)
            {
                this._BitField = (short)(this._BitField | 0x800);
            }
            if (!s.CanSeek)
            {
                this._BitField = (short)(this._BitField | 8);
            }
            bytes[i++] = (byte)(this._BitField & 0xff);
            bytes[i++] = (byte)((this._BitField & 0xff00) >> 8);
            if (this.__FileDataPosition == 0L)
            {
                this._UncompressedSize = 0;
                this._CompressedSize   = 0;
                this._Crc32            = 0;
            }
            this.FigureCompressionMethodForWriting(cycle);
            bytes[i++]     = (byte)(this.CompressionMethod & 0xff);
            bytes[i++]     = (byte)((this.CompressionMethod & 0xff00) >> 8);
            this._TimeBlob = ZipSharedUtilities.DateTimeToPacked(this.LastModified);
            bytes[i++]     = (byte)(this._TimeBlob & 0xff);
            bytes[i++]     = (byte)((this._TimeBlob & 0xff00) >> 8);
            bytes[i++]     = (byte)((this._TimeBlob & 0xff0000) >> 0x10);
            bytes[i++]     = (byte)((this._TimeBlob & 0xff000000L) >> 0x18);
            bytes[i++]     = (byte)(this._Crc32 & 0xff);
            bytes[i++]     = (byte)((this._Crc32 & 0xff00) >> 8);
            bytes[i++]     = (byte)((this._Crc32 & 0xff0000) >> 0x10);
            bytes[i++]     = (byte)((this._Crc32 & 0xff000000L) >> 0x18);
            bytes[i++]     = (byte)(this._CompressedSize & 0xff);
            bytes[i++]     = (byte)((this._CompressedSize & 0xff00) >> 8);
            bytes[i++]     = (byte)((this._CompressedSize & 0xff0000) >> 0x10);
            bytes[i++]     = (byte)((this._CompressedSize & 0xff000000L) >> 0x18);
            bytes[i++]     = (byte)(this._UncompressedSize & 0xff);
            bytes[i++]     = (byte)((this._UncompressedSize & 0xff00) >> 8);
            bytes[i++]     = (byte)((this._UncompressedSize & 0xff0000) >> 0x10);
            bytes[i++]     = (byte)((this._UncompressedSize & 0xff000000L) >> 0x18);
            bytes[i++]     = (byte)(filenameLength & 0xff);
            bytes[i++]     = (byte)((filenameLength & 0xff00) >> 8);
            byte[] extra            = null;
            short  ExtraFieldLength = (extra == null) ? ((short)0) : ((short)extra.Length);

            bytes[i++] = (byte)(ExtraFieldLength & 0xff);
            bytes[i++] = (byte)((ExtraFieldLength & 0xff00) >> 8);
            int j = 0;

            j = 0;
            while ((j < FileNameBytes.Length) && ((i + j) < bytes.Length))
            {
                bytes[i + j] = FileNameBytes[j];
                j++;
            }
            i += j;
            if (extra != null)
            {
                j = 0;
                while (j < extra.Length)
                {
                    bytes[i + j] = extra[j];
                    j++;
                }
                i += j;
            }
            CountingStream counter = s as CountingStream;

            this._RelativeOffsetOfHeader = (counter != null) ? counter.BytesWritten : ((int)s.Position);
            this._LengthOfHeader         = i;
            s.Write(bytes, 0, i);
            this._EntryHeader = new byte[i];
            for (j = 0; j < i; j++)
            {
                this._EntryHeader[j] = bytes[j];
            }
        }