예제 #1
0
 public static byte[] ConvertToArray(int flags, string str)
 {
     return(ZipStrings.ConvertToArray(flags, str));
 }
예제 #2
0
        // Write the local file header
        // TODO: ZipHelperStream.WriteLocalHeader is not yet used and needs checking for ZipFile and ZipOuptutStream usage
        private void WriteLocalHeader(ZipEntry entry, EntryPatchData patchData)
        {
            CompressionMethod method = entry.CompressionMethod;
            bool headerInfoAvailable = true;             // How to get this?
            bool patchEntryHeader    = false;

            this.WriteLEInt(ZipConstants.LocalHeaderSignature);

            this.WriteLEShort(entry.Version);
            this.WriteLEShort(entry.Flags);
            this.WriteLEShort((byte)method);
            this.WriteLEInt((int)entry.DosTime);

            if (headerInfoAvailable == true)
            {
                this.WriteLEInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    this.WriteLEInt(-1);
                    this.WriteLEInt(-1);
                }
                else
                {
                    this.WriteLEInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
                    this.WriteLEInt((int)entry.Size);
                }
            }
            else
            {
                if (patchData != null)
                {
                    patchData.CrcPatchOffset = this.stream_.Position;
                }
                this.WriteLEInt(0);                  // Crc

                if (patchData != null)
                {
                    patchData.SizePatchOffset = this.stream_.Position;
                }

                // For local header both sizes appear in Zip64 Extended Information
                if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
                {
                    this.WriteLEInt(-1);
                    this.WriteLEInt(-1);
                }
                else
                {
                    this.WriteLEInt(0);                      // Compressed size
                    this.WriteLEInt(0);                      // Uncompressed size
                }
            }

            byte[] name = ZipStrings.ConvertToArray(entry.Flags, entry.Name);

            if (name.Length > 0xFFFF)
            {
                throw new ZipException("Entry name too long.");
            }

            var ed = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64 && (headerInfoAvailable || patchEntryHeader))
            {
                ed.StartNewEntry();
                if (headerInfoAvailable)
                {
                    ed.AddLeLong(entry.Size);
                    ed.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    ed.AddLeLong(-1);
                    ed.AddLeLong(-1);
                }
                ed.AddNewEntry(1);

                if (!ed.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }

                if (patchData != null)
                {
                    patchData.SizePatchOffset = ed.CurrentReadIndex;
                }
            }
            else
            {
                ed.Delete(1);
            }

            byte[] extra = ed.GetEntryData();

            this.WriteLEShort(name.Length);
            this.WriteLEShort(extra.Length);

            if (name.Length > 0)
            {
                this.stream_.Write(name, 0, name.Length);
            }

            if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
            {
                patchData.SizePatchOffset += this.stream_.Position;
            }

            if (extra.Length > 0)
            {
                this.stream_.Write(extra, 0, extra.Length);
            }
        }
예제 #3
0
 public static byte[] ConvertToArray(string str)
 {
     return(ZipStrings.ConvertToArray(str));
 }
예제 #4
0
 public static string ConvertToStringExt(int flags, byte[] data)
 {
     return(ZipStrings.ConvertToStringExt(flags, data));
 }
예제 #5
0
 public static string ConvertToString(byte[] data)
 {
     return(ZipStrings.ConvertToString(data));
 }
예제 #6
0
 public static string ConvertToString(byte[] data, int count)
 {
     return(ZipStrings.ConvertToString(data, count));
 }
예제 #7
0
        /// <summary>
        /// Advances to the next entry in the archive
        /// </summary>
        /// <returns>
        /// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
        /// </returns>
        /// <remarks>
        /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
        /// </remarks>
        /// <exception cref="InvalidOperationException">
        /// Input stream is closed
        /// </exception>
        /// <exception cref="ZipException">
        /// Password is not set, password is invalid, compression method is invalid,
        /// version required to extract is not supported
        /// </exception>
        public ZipEntry GetNextEntry()
        {
            if (this.crc == null)
            {
                throw new InvalidOperationException("Closed.");
            }

            if (this.entry != null)
            {
                this.CloseEntry();
            }

            int header = this.inputBuffer.ReadLeInt();

            if (header == ZipConstants.CentralHeaderSignature ||
                header == ZipConstants.EndOfCentralDirectorySignature ||
                header == ZipConstants.CentralHeaderDigitalSignature ||
                header == ZipConstants.ArchiveExtraDataSignature ||
                header == ZipConstants.Zip64CentralFileHeaderSignature)
            {
                // No more individual entries exist
                this.Dispose();
                return(null);
            }

            // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
            // Spanning signature is same as descriptor signature and is untested as yet.
            if ((header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature))
            {
                header = this.inputBuffer.ReadLeInt();
            }

            if (header != ZipConstants.LocalHeaderSignature)
            {
                throw new ZipException("Wrong Local header signature: 0x" + string.Format("{0:X}", header));
            }

            var versionRequiredToExtract = (short)this.inputBuffer.ReadLeShort();

            this.flags  = this.inputBuffer.ReadLeShort();
            this.method = this.inputBuffer.ReadLeShort();
            var dostime = (uint)this.inputBuffer.ReadLeInt();
            int crc2    = this.inputBuffer.ReadLeInt();

            this.csize = this.inputBuffer.ReadLeInt();
            this.size  = this.inputBuffer.ReadLeInt();
            int nameLen  = this.inputBuffer.ReadLeShort();
            int extraLen = this.inputBuffer.ReadLeShort();

            bool isCrypted = (this.flags & 1) == 1;

            byte[] buffer = new byte[nameLen];
            this.inputBuffer.ReadRawBuffer(buffer);

            string name = ZipStrings.ConvertToStringExt(this.flags, buffer);

            this.entry = new ZipEntry(name, versionRequiredToExtract)
            {
                Flags             = this.flags,
                CompressionMethod = (CompressionMethod)this.method
            };

            if ((this.flags & 8) == 0)
            {
                this.entry.Crc            = crc2 & 0xFFFFFFFFL;
                this.entry.Size           = this.size & 0xFFFFFFFFL;
                this.entry.CompressedSize = this.csize & 0xFFFFFFFFL;

                this.entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);
            }
            else
            {
                // This allows for GNU, WinZip and possibly other archives, the PKZIP spec
                // says these values are zero under these circumstances.
                if (crc2 != 0)
                {
                    this.entry.Crc = crc2 & 0xFFFFFFFFL;
                }

                if (this.size != 0)
                {
                    this.entry.Size = this.size & 0xFFFFFFFFL;
                }

                if (this.csize != 0)
                {
                    this.entry.CompressedSize = this.csize & 0xFFFFFFFFL;
                }

                this.entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
            }

            this.entry.DosTime = dostime;

            // If local header requires Zip64 is true then the extended header should contain
            // both values.

            // Handle extra data if present.  This can set/alter some fields of the entry.
            if (extraLen > 0)
            {
                byte[] extra = new byte[extraLen];
                this.inputBuffer.ReadRawBuffer(extra);
                this.entry.ExtraData = extra;
            }

            this.entry.ProcessExtraData(true);
            if (this.entry.CompressedSize >= 0)
            {
                this.csize = this.entry.CompressedSize;
            }

            if (this.entry.Size >= 0)
            {
                this.size = this.entry.Size;
            }

            if (this.method == (int)CompressionMethod.Stored && (!isCrypted && this.csize != this.size || (isCrypted && this.csize - ZipConstants.CryptoHeaderSize != this.size)))
            {
                throw new ZipException("Stored, but compressed != uncompressed");
            }

            // Determine how to handle reading of data if this is attempted.
            if (this.entry.IsCompressionMethodSupported())
            {
                this.internalReader = new ReadDataHandler(this.InitialRead);
            }
            else
            {
                this.internalReader = new ReadDataHandler(this.ReadingNotSupported);
            }

            return(this.entry);
        }