ConvertToArray() public static method

Convert a string to a byte array
public static ConvertToArray ( int flags, string str ) : byte[]
flags int The applicable general purpose bits flags
str string /// String to convert to an array ///
return byte[]
        public override void Finish()
        {
            if (this.entries == null)
            {
                return;
            }
            if (this.curEntry != null)
            {
                this.CloseEntry();
            }
            long noOfEntries = (long)this.entries.Count;
            long num         = 0L;

            foreach (ZipEntry zipEntry in this.entries)
            {
                this.WriteLeInt(33639248);
                this.WriteLeShort(51);
                this.WriteLeShort(zipEntry.Version);
                this.WriteLeShort(zipEntry.Flags);
                this.WriteLeShort((int)((short)zipEntry.CompressionMethodForHeader));
                this.WriteLeInt((int)zipEntry.DosTime);
                this.WriteLeInt((int)zipEntry.Crc);
                if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= (long)((ulong)-1))
                {
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((int)zipEntry.CompressedSize);
                }
                if (zipEntry.IsZip64Forced() || zipEntry.Size >= (long)((ulong)-1))
                {
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((int)zipEntry.Size);
                }
                byte[] array = ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Name);
                if (array.Length > 65535)
                {
                    throw new ZipException("Name too long.");
                }
                ZipExtraData zipExtraData = new ZipExtraData(zipEntry.ExtraData);
                if (zipEntry.CentralHeaderRequiresZip64)
                {
                    zipExtraData.StartNewEntry();
                    if (zipEntry.IsZip64Forced() || zipEntry.Size >= (long)((ulong)-1))
                    {
                        zipExtraData.AddLeLong(zipEntry.Size);
                    }
                    if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= (long)((ulong)-1))
                    {
                        zipExtraData.AddLeLong(zipEntry.CompressedSize);
                    }
                    if (zipEntry.Offset >= (long)((ulong)-1))
                    {
                        zipExtraData.AddLeLong(zipEntry.Offset);
                    }
                    zipExtraData.AddNewEntry(1);
                }
                else
                {
                    zipExtraData.Delete(1);
                }
                if (zipEntry.AESKeySize > 0)
                {
                    ZipOutputStream.AddExtraDataAES(zipEntry, zipExtraData);
                }
                byte[] entryData = zipExtraData.GetEntryData();
                byte[] array2    = (zipEntry.Comment == null) ? new byte[0] : ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Comment);
                if (array2.Length > 65535)
                {
                    throw new ZipException("Comment too long.");
                }
                this.WriteLeShort(array.Length);
                this.WriteLeShort(entryData.Length);
                this.WriteLeShort(array2.Length);
                this.WriteLeShort(0);
                this.WriteLeShort(0);
                if (zipEntry.ExternalFileAttributes != -1)
                {
                    this.WriteLeInt(zipEntry.ExternalFileAttributes);
                }
                else if (zipEntry.IsDirectory)
                {
                    this.WriteLeInt(16);
                }
                else
                {
                    this.WriteLeInt(0);
                }
                if (zipEntry.Offset >= (long)((ulong)-1))
                {
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((int)zipEntry.Offset);
                }
                if (array.Length > 0)
                {
                    this.baseOutputStream_.Write(array, 0, array.Length);
                }
                if (entryData.Length > 0)
                {
                    this.baseOutputStream_.Write(entryData, 0, entryData.Length);
                }
                if (array2.Length > 0)
                {
                    this.baseOutputStream_.Write(array2, 0, array2.Length);
                }
                num += (long)(46 + array.Length + entryData.Length + array2.Length);
            }
            using (ZipHelperStream zipHelperStream = new ZipHelperStream(this.baseOutputStream_))
            {
                zipHelperStream.WriteEndOfCentralDirectory(noOfEntries, num, this.offset, this.zipComment);
            }
            this.entries = null;
        }
Example #2
0
        private void WriteLocalHeader(ZipEntry entry, EntryPatchData patchData)
        {
            CompressionMethod compressionMethod = entry.CompressionMethod;
            bool flag  = true;
            bool flag2 = false;

            this.WriteLEInt(0x4034b50);
            this.WriteLEShort(entry.Version);
            this.WriteLEShort(entry.Flags);
            this.WriteLEShort((byte)compressionMethod);
            this.WriteLEInt((int)entry.DosTime);
            if (flag)
            {
                this.WriteLEInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    this.WriteLEInt(-1);
                    this.WriteLEInt(-1);
                }
                else
                {
                    this.WriteLEInt(entry.IsCrypted ? (((int)entry.CompressedSize) + 12) : ((int)entry.CompressedSize));
                    this.WriteLEInt((int)entry.Size);
                }
            }
            else
            {
                if (patchData != null)
                {
                    patchData.CrcPatchOffset = this.stream_.Position;
                }
                this.WriteLEInt(0);
                if (patchData != null)
                {
                    patchData.SizePatchOffset = this.stream_.Position;
                }
                if (entry.LocalHeaderRequiresZip64 && flag2)
                {
                    this.WriteLEInt(-1);
                    this.WriteLEInt(-1);
                }
                else
                {
                    this.WriteLEInt(0);
                    this.WriteLEInt(0);
                }
            }
            byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (buffer.Length > 0xffff)
            {
                throw new ZipException("Entry name too long.");
            }
            ZipExtraData data = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64 && (flag || flag2))
            {
                data.StartNewEntry();
                if (flag)
                {
                    data.AddLeLong(entry.Size);
                    data.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    data.AddLeLong(-1L);
                    data.AddLeLong(-1L);
                }
                data.AddNewEntry(1);
                if (!data.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }
                if (patchData != null)
                {
                    patchData.SizePatchOffset = data.CurrentReadIndex;
                }
            }
            else
            {
                data.Delete(1);
            }
            byte[] entryData = data.GetEntryData();
            this.WriteLEShort(buffer.Length);
            this.WriteLEShort(entryData.Length);
            if (buffer.Length > 0)
            {
                this.stream_.Write(buffer, 0, buffer.Length);
            }
            if (entry.LocalHeaderRequiresZip64 && flag2)
            {
                patchData.SizePatchOffset += this.stream_.Position;
            }
            if (entryData.Length > 0)
            {
                this.stream_.Write(entryData, 0, entryData.Length);
            }
        }
Example #3
0
        public void PutNextEntry(ZipEntry entry)
        {
            bool hasCrc;

            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (this.entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }
            if (this.curEntry != null)
            {
                this.CloseEntry();
            }
            if (this.entries.Count == 0x7fffffff)
            {
                throw new ZipException("Too many entries for Zip file");
            }
            CompressionMethod compressionMethod = entry.CompressionMethod;
            int defaultCompressionLevel         = this.defaultCompressionLevel;

            entry.Flags          &= 0x800;
            this.patchEntryHeader = false;
            if (entry.Size == 0L)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0L;
                compressionMethod    = CompressionMethod.Stored;
                hasCrc = true;
            }
            else
            {
                hasCrc = (entry.Size >= 0L) && entry.HasCrc;
                if (compressionMethod == CompressionMethod.Stored)
                {
                    if (!hasCrc)
                    {
                        if (!base.CanPatchEntries)
                        {
                            compressionMethod       = CompressionMethod.Deflated;
                            defaultCompressionLevel = 0;
                        }
                    }
                    else
                    {
                        entry.CompressedSize = entry.Size;
                        hasCrc = entry.HasCrc;
                    }
                }
            }
            if (!hasCrc)
            {
                if (!base.CanPatchEntries)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    this.patchEntryHeader = true;
                }
            }
            if (base.Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0L)
                {
                    entry.Flags |= 8;
                }
            }
            entry.Offset            = this.offset;
            entry.CompressionMethod = compressionMethod;
            this.curMethod          = compressionMethod;
            this.sizePatchPos       = -1L;
            if ((this.useZip64_ == ICSharpCode.SharpZipLib.Zip.UseZip64.On) || ((entry.Size < 0L) && (this.useZip64_ == ICSharpCode.SharpZipLib.Zip.UseZip64.Dynamic)))
            {
                entry.ForceZip64();
            }
            this.WriteLeInt(0x4034b50);
            this.WriteLeShort(entry.Version);
            this.WriteLeShort(entry.Flags);
            this.WriteLeShort((byte)entry.CompressionMethodForHeader);
            this.WriteLeInt((int)entry.DosTime);
            if (hasCrc)
            {
                this.WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    this.WriteLeInt(-1);
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt(entry.IsCrypted ? (((int)entry.CompressedSize) + 12) : ((int)entry.CompressedSize));
                    this.WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (this.patchEntryHeader)
                {
                    this.crcPatchPos = base.baseOutputStream_.Position;
                }
                this.WriteLeInt(0);
                if (this.patchEntryHeader)
                {
                    this.sizePatchPos = base.baseOutputStream_.Position;
                }
                if (entry.LocalHeaderRequiresZip64 || this.patchEntryHeader)
                {
                    this.WriteLeInt(-1);
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt(0);
                    this.WriteLeInt(0);
                }
            }
            byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (buffer.Length > 0xffff)
            {
                throw new ZipException("Entry name too long.");
            }
            ZipExtraData extraData = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64)
            {
                extraData.StartNewEntry();
                if (hasCrc)
                {
                    extraData.AddLeLong(entry.Size);
                    extraData.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    extraData.AddLeLong(-1L);
                    extraData.AddLeLong(-1L);
                }
                extraData.AddNewEntry(1);
                if (!extraData.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }
                if (this.patchEntryHeader)
                {
                    this.sizePatchPos = extraData.CurrentReadIndex;
                }
            }
            else
            {
                extraData.Delete(1);
            }
            if (entry.AESKeySize > 0)
            {
                AddExtraDataAES(entry, extraData);
            }
            byte[] entryData = extraData.GetEntryData();
            this.WriteLeShort(buffer.Length);
            this.WriteLeShort(entryData.Length);
            if (buffer.Length > 0)
            {
                base.baseOutputStream_.Write(buffer, 0, buffer.Length);
            }
            if (entry.LocalHeaderRequiresZip64 && this.patchEntryHeader)
            {
                this.sizePatchPos += base.baseOutputStream_.Position;
            }
            if (entryData.Length > 0)
            {
                base.baseOutputStream_.Write(entryData, 0, entryData.Length);
            }
            this.offset += (30 + buffer.Length) + entryData.Length;
            if (entry.AESKeySize > 0)
            {
                this.offset += entry.AESOverheadSize;
            }
            this.curEntry = entry;
            this.crc.Reset();
            if (compressionMethod == CompressionMethod.Deflated)
            {
                base.deflater_.Reset();
                base.deflater_.SetLevel(defaultCompressionLevel);
            }
            this.size = 0L;
            if (entry.IsCrypted)
            {
                if (entry.AESKeySize > 0)
                {
                    this.WriteAESHeader(entry);
                }
                else if (entry.Crc < 0L)
                {
                    this.WriteEncryptionHeader(entry.DosTime << 0x10);
                }
                else
                {
                    this.WriteEncryptionHeader(entry.Crc);
                }
            }
        }
        public void PutNextEntry(ZipEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (this.entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }
            if (this.curEntry != null)
            {
                this.CloseEntry();
            }
            if (this.entries.Count == 2147483647)
            {
                throw new ZipException("Too many entries for Zip file");
            }
            CompressionMethod compressionMethod = entry.CompressionMethod;
            int level = this.defaultCompressionLevel;

            entry.Flags          &= 2048;
            this.patchEntryHeader = false;
            bool flag;

            if (entry.Size == 0L)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0L;
                compressionMethod    = CompressionMethod.Stored;
                flag = true;
            }
            else
            {
                flag = (entry.Size >= 0L && entry.HasCrc && entry.CompressedSize >= 0L);
                if (compressionMethod == CompressionMethod.Stored)
                {
                    if (!flag)
                    {
                        if (!base.CanPatchEntries)
                        {
                            compressionMethod = CompressionMethod.Deflated;
                            level             = 0;
                        }
                    }
                    else
                    {
                        entry.CompressedSize = entry.Size;
                        flag = entry.HasCrc;
                    }
                }
            }
            if (!flag)
            {
                if (!base.CanPatchEntries)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    this.patchEntryHeader = true;
                }
            }
            if (base.Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0L)
                {
                    entry.Flags |= 8;
                }
            }
            entry.Offset            = this.offset;
            entry.CompressionMethod = compressionMethod;
            this.curMethod          = compressionMethod;
            this.sizePatchPos       = -1L;
            if (this.useZip64_ == UseZip64.On || (entry.Size < 0L && this.useZip64_ == UseZip64.Dynamic))
            {
                entry.ForceZip64();
            }
            this.WriteLeInt(67324752);
            this.WriteLeShort(entry.Version);
            this.WriteLeShort(entry.Flags);
            this.WriteLeShort((int)((byte)entry.CompressionMethodForHeader));
            this.WriteLeInt((int)entry.DosTime);
            if (flag)
            {
                this.WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    this.WriteLeInt(-1);
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt((!entry.IsCrypted) ? ((int)entry.CompressedSize) : ((int)entry.CompressedSize + 12));
                    this.WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (this.patchEntryHeader)
                {
                    this.crcPatchPos = this.baseOutputStream_.Position;
                }
                this.WriteLeInt(0);
                if (this.patchEntryHeader)
                {
                    this.sizePatchPos = this.baseOutputStream_.Position;
                }
                if (entry.LocalHeaderRequiresZip64 || this.patchEntryHeader)
                {
                    this.WriteLeInt(-1);
                    this.WriteLeInt(-1);
                }
                else
                {
                    this.WriteLeInt(0);
                    this.WriteLeInt(0);
                }
            }
            byte[] array = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (array.Length > 65535)
            {
                throw new ZipException("Entry name too long.");
            }
            ZipExtraData zipExtraData = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64)
            {
                zipExtraData.StartNewEntry();
                if (flag)
                {
                    zipExtraData.AddLeLong(entry.Size);
                    zipExtraData.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    zipExtraData.AddLeLong(-1L);
                    zipExtraData.AddLeLong(-1L);
                }
                zipExtraData.AddNewEntry(1);
                if (!zipExtraData.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }
                if (this.patchEntryHeader)
                {
                    this.sizePatchPos = (long)zipExtraData.CurrentReadIndex;
                }
            }
            else
            {
                zipExtraData.Delete(1);
            }
            if (entry.AESKeySize > 0)
            {
                ZipOutputStream.AddExtraDataAES(entry, zipExtraData);
            }
            byte[] entryData = zipExtraData.GetEntryData();
            this.WriteLeShort(array.Length);
            this.WriteLeShort(entryData.Length);
            if (array.Length > 0)
            {
                this.baseOutputStream_.Write(array, 0, array.Length);
            }
            if (entry.LocalHeaderRequiresZip64 && this.patchEntryHeader)
            {
                this.sizePatchPos += this.baseOutputStream_.Position;
            }
            if (entryData.Length > 0)
            {
                this.baseOutputStream_.Write(entryData, 0, entryData.Length);
            }
            this.offset += (long)(30 + array.Length + entryData.Length);
            if (entry.AESKeySize > 0)
            {
                this.offset += (long)entry.AESOverheadSize;
            }
            this.curEntry = entry;
            this.crc.Reset();
            if (compressionMethod == CompressionMethod.Deflated)
            {
                this.deflater_.Reset();
                this.deflater_.SetLevel(level);
            }
            this.size = 0L;
            if (entry.IsCrypted)
            {
                if (entry.AESKeySize > 0)
                {
                    this.WriteAESHeader(entry);
                }
                else if (entry.Crc < 0L)
                {
                    this.WriteEncryptionHeader(entry.DosTime << 16);
                }
                else
                {
                    this.WriteEncryptionHeader(entry.Crc);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Starts a new Zip entry. It automatically closes the previous
        /// entry if present.
        /// All entry elements bar name are optional, but must be correct if present.
        /// If the compression method is stored and the output is not patchable
        /// the compression for that entry is automatically changed to deflate level 0
        /// </summary>
        /// <param name="entry">
        /// the entry.
        /// </param>
        /// <exception cref="System.IO.IOException">
        /// if an I/O error occured.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// if stream was finished
        /// </exception>
        /// <exception cref="ZipException">
        /// Too many entries in the Zip file<br/>
        /// Entry name is too long<br/>
        /// Finish has already been called<br/>
        /// </exception>
        public void PutNextEntry(ZipEntry entry)
        {
            if (entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            if (entries.Count >= 0xffff)
            {
                throw new ZipException("Too many entries for Zip file");
            }

            CompressionMethod method = entry.CompressionMethod;
            int compressionLevel     = defaultCompressionLevel;

            entry.Flags      = 0;
            patchEntryHeader = false;
            bool headerInfoAvailable = true;

            if (method == CompressionMethod.Stored)
            {
                if (entry.CompressedSize >= 0)
                {
                    if (entry.Size < 0)
                    {
                        entry.Size = entry.CompressedSize;
                    }
                    else if (entry.Size != entry.CompressedSize)
                    {
                        throw new ZipException("Method STORED, but compressed size != size");
                    }
                }
                else
                {
                    if (entry.Size >= 0)
                    {
                        entry.CompressedSize = entry.Size;
                    }
                }

                if (entry.Size < 0 || entry.Crc < 0)
                {
                    if (CanPatchEntries == true)
                    {
                        headerInfoAvailable = false;
                    }
                    else
                    {
                        method           = CompressionMethod.Deflated;
                        compressionLevel = 0;
                    }
                }
            }

            if (method == CompressionMethod.Deflated)
            {
                if (entry.Size == 0)
                {
                    entry.CompressedSize = entry.Size;
                    entry.Crc            = 0;
                    method = CompressionMethod.Stored;
                }
                else if (entry.CompressedSize < 0 || entry.Size < 0 || entry.Crc < 0)
                {
                    headerInfoAvailable = false;
                }
            }

            if (headerInfoAvailable == false)
            {
                if (CanPatchEntries == false)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    patchEntryHeader = true;
                }
            }

            if (Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0)
                {
                    entry.Flags |= 8;
                }
            }
            entry.Offset            = (int)offset;
            entry.CompressionMethod = (CompressionMethod)method;

            curMethod = method;

            // Write the local file header
            WriteLeInt(ZipConstants.LOCSIG);

            WriteLeShort(entry.Version);
            WriteLeShort(entry.Flags);
            WriteLeShort((byte)method);
            WriteLeInt((int)entry.DosTime);
            if (headerInfoAvailable == true)
            {
                WriteLeInt((int)entry.Crc);
                WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CRYPTO_HEADER_SIZE : (int)entry.CompressedSize);
                WriteLeInt((int)entry.Size);
            }
            else
            {
                if (patchEntryHeader == true)
                {
                    headerPatchPos = baseOutputStream.Position;
                }
                WriteLeInt(0);                  // Crc
                WriteLeInt(0);                  // Compressed size
                WriteLeInt(0);                  // Uncompressed size
            }

            byte[] name = ZipConstants.ConvertToArray(entry.Name);

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

            byte[] extra = entry.ExtraData;
            if (extra == null)
            {
                extra = new byte[0];
            }

            if (extra.Length > 0xFFFF)
            {
                throw new ZipException("Extra data too long.");
            }

            WriteLeShort(name.Length);
            WriteLeShort(extra.Length);
            baseOutputStream.Write(name, 0, name.Length);
            baseOutputStream.Write(extra, 0, extra.Length);

            offset += ZipConstants.LOCHDR + name.Length + extra.Length;

            // Activate the entry.
            curEntry = entry;
            crc.Reset();
            if (method == CompressionMethod.Deflated)
            {
                def.Reset();
                def.SetLevel(compressionLevel);
            }
            size = 0;

            if (entry.IsCrypted == true)
            {
                if (entry.Crc < 0)                                      // so testing Zip will says its ok
                {
                    WriteEncryptionHeader(entry.DosTime << 16);
                }
                else
                {
                    WriteEncryptionHeader(entry.Crc);
                }
            }
        }
Example #6
0
 private int InitialRead(byte[] destination, int offset, int count)
 {
     if (!this.CanDecompressEntry)
     {
         throw new ZipException("Library cannot extract this entry. Version required is (" + this.entry.Version.ToString() + ")");
     }
     if (!this.entry.IsCrypted)
     {
         base.inputBuffer.CryptoTransform = null;
     }
     else
     {
         if (this.password == null)
         {
             throw new ZipException("No password set.");
         }
         base.inputBuffer.CryptoTransform = new PkzipClassicManaged().CreateDecryptor(PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(this.password)), null);
         byte[] outBuffer = new byte[12];
         base.inputBuffer.ReadClearTextBuffer(outBuffer, 0, 12);
         if (outBuffer[11] != this.entry.CryptoCheckValue)
         {
             throw new ZipException("Invalid password");
         }
         if (base.csize >= 12)
         {
             base.csize -= 12;
         }
         else if ((this.entry.Flags & 8) == 0)
         {
             throw new ZipException($"Entry compressed size {base.csize} too small for encryption");
         }
     }
     if ((base.csize <= 0L) && ((this.flags & 8) == 0))
     {
         this.internalReader = new ReadDataHandler(this.ReadingNotAvailable);
         return(0);
     }
     if ((this.method == 8) && (base.inputBuffer.Available > 0))
     {
         base.inputBuffer.SetInflaterInput(base.inf);
     }
     this.internalReader = new ReadDataHandler(this.BodyRead);
     return(this.BodyRead(destination, offset, count));
 }
Example #7
0
        /// <summary>
        /// Starts a new Zip entry. It automatically closes the previous
        /// entry if present.  If the compression method is stored, the entry
        /// must have a valid size and crc, otherwise all elements (except
        /// name) are optional, but must be correct if present.  If the time
        /// is not set in the entry, the current time is used.
        /// </summary>
        /// <param name="entry">
        /// the entry.
        /// </param>
        /// <exception cref="System.IO.IOException">
        /// if an I/O error occured.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// if stream was finished
        /// </exception>
        public void PutNextEntry(ZipEntry entry)
        {
            if (entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }

            CompressionMethod method = entry.CompressionMethod;
            int flags = 0;

            entry.IsCrypted = Password != null;
            switch (method)
            {
            case CompressionMethod.Stored:
                if (entry.CompressedSize >= 0)
                {
                    if (entry.Size < 0)
                    {
                        entry.Size = entry.CompressedSize;
                    }
                    else if (entry.Size != entry.CompressedSize)
                    {
                        throw new ZipException("Method STORED, but compressed size != size");
                    }
                }
                else
                {
                    entry.CompressedSize = entry.Size;
                }

                if (entry.IsCrypted)
                {
                    entry.CompressedSize += 12;
                }

                if (entry.Size < 0)
                {
                    throw new ZipException("Method STORED, but size not set");
                }
                else if (entry.Crc < 0)
                {
                    throw new ZipException("Method STORED, but crc not set");
                }
                break;

            case CompressionMethod.Deflated:
                if (entry.CompressedSize < 0 || entry.Size < 0 || entry.Crc < 0)
                {
                    flags |= 8;
                }
                break;
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            //			if (entry.DosTime < 0) {
            //				entry.Time = System.Environment.TickCount;
            //			}
            if (entry.IsCrypted)
            {
                flags |= 1;
            }
            entry.Flags             = flags;
            entry.Offset            = offset;
            entry.CompressionMethod = (CompressionMethod)method;

            curMethod = method;
            // Write the local file header
            WriteLeInt(ZipConstants.LOCSIG);

            // write ZIP version
            WriteLeShort(method == CompressionMethod.Stored ? ZIP_STORED_VERSION : ZIP_DEFLATED_VERSION);
            if ((flags & 8) == 0)
            {
                WriteLeShort(flags);
                WriteLeShort((byte)method);
                WriteLeInt((int)entry.DosTime);
                WriteLeInt((int)entry.Crc);
                WriteLeInt((int)entry.CompressedSize);
                WriteLeInt((int)entry.Size);
            }
            else
            {
                if (baseOutputStream.CanSeek)
                {
                    shouldWriteBack = true;
                    WriteLeShort((short)(flags & ~8));
                }
                else
                {
                    shouldWriteBack = false;
                    WriteLeShort(flags);
                }
                WriteLeShort((byte)method);
                WriteLeInt((int)entry.DosTime);
                if (baseOutputStream.CanSeek)
                {
                    seekPos = baseOutputStream.Position;
                }
                WriteLeInt(0);
                WriteLeInt(0);
                WriteLeInt(0);
            }
            byte[] name = ZipConstants.ConvertToArray(entry.Name);

            if (name.Length > 0xFFFF)
            {
                throw new ZipException("Name too long.");
            }
            byte[] extra = entry.ExtraData;
            if (extra == null)
            {
                extra = new byte[0];
            }
            if (extra.Length > 0xFFFF)
            {
                throw new ZipException("Extra data too long.");
            }

            WriteLeShort(name.Length);
            WriteLeShort(extra.Length);
            baseOutputStream.Write(name, 0, name.Length);
            baseOutputStream.Write(extra, 0, extra.Length);

            if (Password != null)
            {
                InitializePassword(Password);
                byte[] cryptbuffer = new byte[12];
                Random rnd         = new Random();
                for (int i = 0; i < cryptbuffer.Length; ++i)
                {
                    cryptbuffer[i] = (byte)rnd.Next();
                }
                EncryptBlock(cryptbuffer, 0, cryptbuffer.Length);
                baseOutputStream.Write(cryptbuffer, 0, cryptbuffer.Length);
            }
            offset += ZipConstants.LOCHDR + name.Length + extra.Length;

            /* Activate the entry. */
            curEntry = entry;
            crc.Reset();
            if (method == CompressionMethod.Deflated)
            {
                def.Reset();
            }
            size = 0;
        }
        /// <summary>
        /// Starts a new Zip entry. It automatically closes the previous
        /// entry if present.
        /// All entry elements bar name are optional, but must be correct if present.
        /// If the compression method is stored and the output is not patchable
        /// the compression for that entry is automatically changed to deflate level 0
        /// </summary>
        /// <param name="entry">
        /// the entry.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// if entry passed is null.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// if an I/O error occured.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// if stream was finished
        /// </exception>
        /// <exception cref="ZipException">
        /// Too many entries in the Zip file<br/>
        /// Entry name is too long<br/>
        /// Finish has already been called<br/>
        /// </exception>
        public void PutNextEntry(ZipEntryM entry)
        {
            if (entry == null)
            {
                //throw new ArgumentNullException(//nameof//(entry));
            }

            if (entries == null)
            {
                //throw new InvalidOperationException("ZipOutputStream was finished");
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            if (entries.Count == int.MaxValue)
            {
                //throw new ZipException("Too many entries for Zip file");
            }

            CompressionMethod method = entry.CompressionMethod;
            int compressionLevel     = defaultCompressionLevel;

            // Clear flags that the library manages internally
            entry.Flags     &= (int)GeneralBitFlags.UnicodeText;
            patchEntryHeader = false;

            bool headerInfoAvailable;

            // No need to compress - definitely no data.
            if (entry.Size == 0)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0;
                method = CompressionMethod.Stored;
                headerInfoAvailable = true;
            }
            else
            {
                headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc && entry.CompressedSize >= 0;

                // Switch to deflation if storing isnt possible.
                if (method == CompressionMethod.Stored)
                {
                    if (!headerInfoAvailable)
                    {
                        if (!CanPatchEntries)
                        {
                            // Can't patch entries so storing is not possible.
                            method           = CompressionMethod.Deflated;
                            compressionLevel = 0;
                        }
                    }
                    else                       // entry.size must be > 0
                    {
                        entry.CompressedSize = entry.Size;
                        headerInfoAvailable  = entry.HasCrc;
                    }
                }
            }

            if (headerInfoAvailable == false)
            {
                if (CanPatchEntries == false)
                {
                    // Only way to record size and compressed size is to append a data descriptor
                    // after compressed data.

                    // Stored entries of this form have already been converted to deflating.
                    entry.Flags |= 8;
                }
                else
                {
                    patchEntryHeader = true;
                }
            }

            if (Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0)
                {
                    // Need to append a data descriptor as the crc isnt available for use
                    // with encryption, the date is used instead.  Setting the flag
                    // indicates this to the decompressor.
                    entry.Flags |= 8;
                }
            }

            entry.Offset            = offset;
            entry.CompressionMethod = (CompressionMethod)method;

            curMethod    = method;
            sizePatchPos = -1;

            if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic)))
            {
                entry.ForceZip64();
            }

            // Write the local file header
            WriteLeInt(ZipConstants.LocalHeaderSignature);

            WriteLeShort(entry.Version);
            WriteLeShort(entry.Flags);
            WriteLeShort((byte)entry.CompressionMethodForHeader);
            WriteLeInt((int)entry.DosTime);

            // TODO: Refactor header writing.  Its done in several places.
            if (headerInfoAvailable)
            {
                WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
                    WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (patchEntryHeader)
                {
                    crcPatchPos = baseOutputStream_.Position;
                }
                WriteLeInt(0);                  // Crc

                if (patchEntryHeader)
                {
                    sizePatchPos = baseOutputStream_.Position;
                }

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

            byte[] name = ZipConstants.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)
            {
                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 (patchEntryHeader)
                {
                    sizePatchPos = ed.CurrentReadIndex;
                }
            }
            else
            {
                ed.Delete(1);
            }

            if (entry.AESKeySize > 0)
            {
                AddExtraDataAES(entry, ed);
            }
            byte[] extra = ed.GetEntryData();

            WriteLeShort(name.Length);
            WriteLeShort(extra.Length);

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

            if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
            {
                sizePatchPos += baseOutputStream_.Position;
            }

            if (extra.Length > 0)
            {
                baseOutputStream_.Write(extra, 0, extra.Length);
            }

            offset += ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length;
            // Fix offsetOfCentraldir for AES
            if (entry.AESKeySize > 0)
            {
                offset += entry.AESOverheadSize;
            }

            // Activate the entry.
            curEntry = entry;
            crc.Reset();
            if (method == CompressionMethod.Deflated)
            {
                deflater_.Reset();
                deflater_.SetLevel(compressionLevel);
            }
            size = 0;

            if (entry.IsCrypted)
            {
                if (entry.AESKeySize > 0)
                {
                    WriteAESHeader(entry);
                }
                else
                {
                    if (entry.Crc < 0)                                  // so testing Zip will says its ok
                    {
                        WriteEncryptionHeader(entry.DosTime << 16);
                    }
                    else
                    {
                        WriteEncryptionHeader(entry.Crc);
                    }
                }
            }
        }
Example #9
0
 public override void Finish()
 {
     if (this.entries != null)
     {
         if (this.curEntry != null)
         {
             this.CloseEntry();
         }
         int num  = 0;
         int num2 = 0;
         foreach (ZipEntry entry in this.entries)
         {
             CompressionMethod compressionMethod = entry.CompressionMethod;
             this.WriteLeInt(0x2014b50);
             this.WriteLeShort(20);
             this.WriteLeShort(entry.Version);
             this.WriteLeShort(entry.Flags);
             this.WriteLeShort((short)compressionMethod);
             this.WriteLeInt((int)entry.DosTime);
             this.WriteLeInt((int)entry.Crc);
             this.WriteLeInt((int)entry.CompressedSize);
             this.WriteLeInt((int)entry.Size);
             byte[] buffer = ZipConstants.ConvertToArray(entry.Name);
             if (buffer.Length > 0xffff)
             {
                 throw new ZipException("Name too long.");
             }
             byte[] extraData = entry.ExtraData;
             if (extraData == null)
             {
                 extraData = new byte[0];
             }
             byte[] buffer3 = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Comment) : new byte[0];
             if (buffer3.Length > 0xffff)
             {
                 throw new ZipException("Comment too long.");
             }
             this.WriteLeShort(buffer.Length);
             this.WriteLeShort(extraData.Length);
             this.WriteLeShort(buffer3.Length);
             this.WriteLeShort(0);
             this.WriteLeShort(0);
             if (entry.ExternalFileAttributes != -1)
             {
                 this.WriteLeInt(entry.ExternalFileAttributes);
             }
             else if (entry.IsDirectory)
             {
                 this.WriteLeInt(0x10);
             }
             else
             {
                 this.WriteLeInt(0);
             }
             this.WriteLeInt(entry.Offset);
             base.baseOutputStream.Write(buffer, 0, buffer.Length);
             base.baseOutputStream.Write(extraData, 0, extraData.Length);
             base.baseOutputStream.Write(buffer3, 0, buffer3.Length);
             num++;
             num2 += ((0x2e + buffer.Length) + extraData.Length) + buffer3.Length;
         }
         this.WriteLeInt(0x6054b50);
         this.WriteLeShort(0);
         this.WriteLeShort(0);
         this.WriteLeShort(num);
         this.WriteLeShort(num);
         this.WriteLeInt(num2);
         this.WriteLeInt((int)this.offset);
         this.WriteLeShort(this.zipComment.Length);
         base.baseOutputStream.Write(this.zipComment, 0, this.zipComment.Length);
         base.baseOutputStream.Flush();
         this.entries = null;
     }
 }
Example #10
0
        public void PutNextEntry(ZipEntry entry)
        {
            if (this.entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }
            if (this.curEntry != null)
            {
                this.CloseEntry();
            }
            if (this.entries.Count >= 0xffff)
            {
                throw new ZipException("Too many entries for Zip file");
            }
            CompressionMethod compressionMethod = entry.CompressionMethod;
            int defaultCompressionLevel         = this.defaultCompressionLevel;

            entry.Flags           = 0;
            this.patchEntryHeader = false;
            bool flag = true;

            switch (compressionMethod)
            {
            case CompressionMethod.Stored:
                if (entry.CompressedSize >= 0L)
                {
                    if (entry.Size >= 0L)
                    {
                        if (entry.Size != entry.CompressedSize)
                        {
                            throw new ZipException("Method STORED, but compressed size != size");
                        }
                    }
                    else
                    {
                        entry.Size = entry.CompressedSize;
                    }
                }
                else if (entry.Size >= 0L)
                {
                    entry.CompressedSize = entry.Size;
                }
                if ((entry.Size < 0L) || (entry.Crc < 0L))
                {
                    if (base.CanPatchEntries)
                    {
                        flag = false;
                    }
                    else
                    {
                        compressionMethod       = CompressionMethod.Deflated;
                        defaultCompressionLevel = 0;
                    }
                }
                break;

            case CompressionMethod.Deflated:
                if (entry.Size == 0L)
                {
                    entry.CompressedSize = entry.Size;
                    entry.Crc            = 0L;
                    compressionMethod    = CompressionMethod.Stored;
                }
                else if (((entry.CompressedSize < 0L) || (entry.Size < 0L)) || (entry.Crc < 0L))
                {
                    flag = false;
                }
                break;
            }
            if (!flag)
            {
                if (!base.CanPatchEntries)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    this.patchEntryHeader = true;
                }
            }
            if (base.Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0L)
                {
                    entry.Flags |= 8;
                }
            }
            entry.Offset            = (int)this.offset;
            entry.CompressionMethod = compressionMethod;
            this.curMethod          = compressionMethod;
            this.WriteLeInt(0x4034b50);
            this.WriteLeShort(entry.Version);
            this.WriteLeShort(entry.Flags);
            this.WriteLeShort((byte)compressionMethod);
            this.WriteLeInt((int)entry.DosTime);
            if (flag)
            {
                this.WriteLeInt((int)entry.Crc);
                this.WriteLeInt(entry.IsCrypted ? (((int)entry.CompressedSize) + 12) : ((int)entry.CompressedSize));
                this.WriteLeInt((int)entry.Size);
            }
            else
            {
                if (this.patchEntryHeader)
                {
                    this.headerPatchPos = base.baseOutputStream.Position;
                }
                this.WriteLeInt(0);
                this.WriteLeInt(0);
                this.WriteLeInt(0);
            }
            byte[] buffer = ZipConstants.ConvertToArray(entry.Name);
            if (buffer.Length > 0xffff)
            {
                throw new ZipException("Entry name too long.");
            }
            byte[] extraData = entry.ExtraData;
            if (extraData == null)
            {
                extraData = new byte[0];
            }
            if (extraData.Length > 0xffff)
            {
                throw new ZipException("Extra data too long.");
            }
            this.WriteLeShort(buffer.Length);
            this.WriteLeShort(extraData.Length);
            base.baseOutputStream.Write(buffer, 0, buffer.Length);
            base.baseOutputStream.Write(extraData, 0, extraData.Length);
            this.offset  += (30 + buffer.Length) + extraData.Length;
            this.curEntry = entry;
            this.crc.Reset();
            if (compressionMethod == CompressionMethod.Deflated)
            {
                base.def.Reset();
                base.def.SetLevel(defaultCompressionLevel);
            }
            this.size = 0L;
            if (entry.IsCrypted)
            {
                if (entry.Crc < 0L)
                {
                    this.WriteEncryptionHeader(entry.DosTime << 0x10);
                }
                else
                {
                    this.WriteEncryptionHeader(entry.Crc);
                }
            }
        }
Example #11
0
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }
            if (curEntry != null)
            {
                CloseEntry();
            }
            long noOfEntries = entries.get_Count();
            long num         = 0L;

            global::System.Collections.IEnumerator enumerator = entries.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    ZipEntry zipEntry = (ZipEntry)enumerator.get_Current();
                    WriteLeInt(33639248);
                    WriteLeShort(51);
                    WriteLeShort(zipEntry.Version);
                    WriteLeShort(zipEntry.Flags);
                    WriteLeShort((short)zipEntry.CompressionMethodForHeader);
                    WriteLeInt((int)zipEntry.DosTime);
                    WriteLeInt((int)zipEntry.Crc);
                    if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= 4294967295u)
                    {
                        WriteLeInt(-1);
                    }
                    else
                    {
                        WriteLeInt((int)zipEntry.CompressedSize);
                    }
                    if (zipEntry.IsZip64Forced() || zipEntry.Size >= 4294967295u)
                    {
                        WriteLeInt(-1);
                    }
                    else
                    {
                        WriteLeInt((int)zipEntry.Size);
                    }
                    byte[] array = ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Name);
                    if (array.Length > 65535)
                    {
                        throw new ZipException("Name too long.");
                    }
                    ZipExtraData zipExtraData = new ZipExtraData(zipEntry.ExtraData);
                    if (zipEntry.CentralHeaderRequiresZip64)
                    {
                        zipExtraData.StartNewEntry();
                        if (zipEntry.IsZip64Forced() || zipEntry.Size >= 4294967295u)
                        {
                            zipExtraData.AddLeLong(zipEntry.Size);
                        }
                        if (zipEntry.IsZip64Forced() || zipEntry.CompressedSize >= 4294967295u)
                        {
                            zipExtraData.AddLeLong(zipEntry.CompressedSize);
                        }
                        if (zipEntry.Offset >= 4294967295u)
                        {
                            zipExtraData.AddLeLong(zipEntry.Offset);
                        }
                        zipExtraData.AddNewEntry(1);
                    }
                    else
                    {
                        zipExtraData.Delete(1);
                    }
                    byte[] entryData = zipExtraData.GetEntryData();
                    byte[] array2    = ((zipEntry.Comment != null) ? ZipConstants.ConvertToArray(zipEntry.Flags, zipEntry.Comment) : new byte[0]);
                    if (array2.Length > 65535)
                    {
                        throw new ZipException("Comment too long.");
                    }
                    WriteLeShort(array.Length);
                    WriteLeShort(entryData.Length);
                    WriteLeShort(array2.Length);
                    WriteLeShort(0);
                    WriteLeShort(0);
                    if (zipEntry.ExternalFileAttributes != -1)
                    {
                        WriteLeInt(zipEntry.ExternalFileAttributes);
                    }
                    else if (zipEntry.IsDirectory)
                    {
                        WriteLeInt(16);
                    }
                    else
                    {
                        WriteLeInt(0);
                    }
                    if (zipEntry.Offset >= 4294967295u)
                    {
                        WriteLeInt(-1);
                    }
                    else
                    {
                        WriteLeInt((int)zipEntry.Offset);
                    }
                    if (array.Length > 0)
                    {
                        baseOutputStream_.Write(array, 0, array.Length);
                    }
                    if (entryData.Length > 0)
                    {
                        baseOutputStream_.Write(entryData, 0, entryData.Length);
                    }
                    if (array2.Length > 0)
                    {
                        baseOutputStream_.Write(array2, 0, array2.Length);
                    }
                    num += 46 + array.Length + entryData.Length + array2.Length;
                }
            }
            finally
            {
                global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            ZipHelperStream zipHelperStream = new ZipHelperStream(baseOutputStream_);

            try
            {
                zipHelperStream.WriteEndOfCentralDirectory(noOfEntries, num, offset, zipComment);
            }
            finally
            {
                ((global::System.IDisposable)zipHelperStream)?.Dispose();
            }
            entries = null;
        }
Example #12
0
        public void PutNextEntry(ZipEntry entry)
        {
            //IL_0008: Unknown result type (might be due to invalid IL or missing references)
            //IL_001b: Unknown result type (might be due to invalid IL or missing references)
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }
            if (curEntry != null)
            {
                CloseEntry();
            }
            if (entries.get_Count() == 2147483647)
            {
                throw new ZipException("Too many entries for Zip file");
            }
            CompressionMethod compressionMethod = entry.CompressionMethod;
            int level = defaultCompressionLevel;

            entry.Flags     &= 2048;
            patchEntryHeader = false;
            bool flag;

            if (entry.Size == 0)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0L;
                compressionMethod    = CompressionMethod.Stored;
                flag = true;
            }
            else
            {
                flag = entry.Size >= 0 && entry.HasCrc;
                if (compressionMethod == CompressionMethod.Stored)
                {
                    if (!flag)
                    {
                        if (!base.CanPatchEntries)
                        {
                            compressionMethod = CompressionMethod.Deflated;
                            level             = 0;
                        }
                    }
                    else
                    {
                        entry.CompressedSize = entry.Size;
                        flag = entry.HasCrc;
                    }
                }
            }
            if (!flag)
            {
                if (!base.CanPatchEntries)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    patchEntryHeader = true;
                }
            }
            if (base.Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0)
                {
                    entry.Flags |= 8;
                }
            }
            entry.Offset            = offset;
            entry.CompressionMethod = compressionMethod;
            curMethod    = compressionMethod;
            sizePatchPos = -1L;
            if (useZip64_ == UseZip64.On || (entry.Size < 0 && useZip64_ == UseZip64.Dynamic))
            {
                entry.ForceZip64();
            }
            WriteLeInt(67324752);
            WriteLeShort(entry.Version);
            WriteLeShort(entry.Flags);
            WriteLeShort((byte)entry.CompressionMethodForHeader);
            WriteLeInt((int)entry.DosTime);
            if (flag)
            {
                WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)(entry.IsCrypted ? ((int)entry.CompressedSize + 12) : entry.CompressedSize));
                    WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (patchEntryHeader)
                {
                    crcPatchPos = baseOutputStream_.get_Position();
                }
                WriteLeInt(0);
                if (patchEntryHeader)
                {
                    sizePatchPos = baseOutputStream_.get_Position();
                }
                if (entry.LocalHeaderRequiresZip64 || patchEntryHeader)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(0);
                    WriteLeInt(0);
                }
            }
            byte[] array = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (array.Length > 65535)
            {
                throw new ZipException("Entry name too long.");
            }
            ZipExtraData zipExtraData = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64)
            {
                zipExtraData.StartNewEntry();
                if (flag)
                {
                    zipExtraData.AddLeLong(entry.Size);
                    zipExtraData.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    zipExtraData.AddLeLong(-1L);
                    zipExtraData.AddLeLong(-1L);
                }
                zipExtraData.AddNewEntry(1);
                if (!zipExtraData.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }
                if (patchEntryHeader)
                {
                    sizePatchPos = zipExtraData.CurrentReadIndex;
                }
            }
            else
            {
                zipExtraData.Delete(1);
            }
            byte[] entryData = zipExtraData.GetEntryData();
            WriteLeShort(array.Length);
            WriteLeShort(entryData.Length);
            if (array.Length > 0)
            {
                baseOutputStream_.Write(array, 0, array.Length);
            }
            if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
            {
                sizePatchPos += baseOutputStream_.get_Position();
            }
            if (entryData.Length > 0)
            {
                baseOutputStream_.Write(entryData, 0, entryData.Length);
            }
            offset += 30 + array.Length + entryData.Length;
            if (entry.AESKeySize > 0)
            {
                offset += entry.AESOverheadSize;
            }
            curEntry = entry;
            crc.Reset();
            if (compressionMethod == CompressionMethod.Deflated)
            {
                deflater_.Reset();
                deflater_.SetLevel(level);
            }
            size = 0L;
            if (entry.IsCrypted)
            {
                if (entry.Crc < 0)
                {
                    WriteEncryptionHeader(entry.DosTime << 16);
                }
                else
                {
                    WriteEncryptionHeader(entry.Crc);
                }
            }
        }
Example #13
0
        public void PutNextEntry(ZipEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

            if (entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            if (entries.Count == int.MaxValue)
            {
                throw new ZipException("Too many entries for Zip file");
            }

            CompressionMethod method = entry.CompressionMethod;
            int compressionLevel     = defaultCompressionLevel;


            entry.Flags     &= (int)GeneralBitFlags.UnicodeText;
            patchEntryHeader = false;

            bool headerInfoAvailable;

            if (entry.Size == 0)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0;
                method = CompressionMethod.Stored;
                headerInfoAvailable = true;
            }
            else
            {
                headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc;


                if (method == CompressionMethod.Stored)
                {
                    if (!headerInfoAvailable)
                    {
                        if (!CanPatchEntries)
                        {
                            method           = CompressionMethod.Deflated;
                            compressionLevel = 0;
                        }
                    }
                    else
                    {
                        entry.CompressedSize = entry.Size;
                        headerInfoAvailable  = entry.HasCrc;
                    }
                }
            }

            if (headerInfoAvailable == false)
            {
                if (CanPatchEntries == false)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    patchEntryHeader = true;
                }
            }

            if (Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0)
                {
                    entry.Flags |= 8;
                }
            }

            entry.Offset            = offset;
            entry.CompressionMethod = (CompressionMethod)method;

            curMethod    = method;
            sizePatchPos = -1;

            if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic)))
            {
                entry.ForceZip64();
            }


            WriteLeInt(ZipConstants.LocalHeaderSignature);

            WriteLeShort(entry.Version);
            WriteLeShort(entry.Flags);
            WriteLeShort((byte)method);
            WriteLeInt((int)entry.DosTime);


            if (headerInfoAvailable == true)
            {
                WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
                    WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (patchEntryHeader == true)
                {
                    crcPatchPos = baseOutputStream_.Position;
                }
                WriteLeInt(0);

                if (patchEntryHeader)
                {
                    sizePatchPos = baseOutputStream_.Position;
                }


                if (entry.LocalHeaderRequiresZip64 || patchEntryHeader)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(0);
                    WriteLeInt(0);
                }
            }

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

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

            ZipExtraData ed = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64)
            {
                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 (patchEntryHeader)
                {
                    sizePatchPos = ed.CurrentReadIndex;
                }
            }
            else
            {
                ed.Delete(1);
            }

            byte[] extra = ed.GetEntryData();

            WriteLeShort(name.Length);
            WriteLeShort(extra.Length);

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

            if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
            {
                sizePatchPos += baseOutputStream_.Position;
            }

            if (extra.Length > 0)
            {
                baseOutputStream_.Write(extra, 0, extra.Length);
            }

            offset += ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length;


            curEntry = entry;
            crc.Reset();
            if (method == CompressionMethod.Deflated)
            {
                deflater_.Reset();
                deflater_.SetLevel(compressionLevel);
            }
            size = 0;

            if (entry.IsCrypted == true)
            {
                if (entry.Crc < 0)
                {
                    WriteEncryptionHeader(entry.DosTime << 16);
                }
                else
                {
                    WriteEncryptionHeader(entry.Crc);
                }
            }
        }
Example #14
0
        // Write the local file header
        // TODO: ZipHelperStream.WriteLocalHeader is not yet used and needs checking for ZipFile and ZipOuptutStream usage
        void WriteLocalHeader(ZipEntry entry, EntryPatchData patchData)
        {
            CompressionMethod method = entry.CompressionMethod;
            bool headerInfoAvailable = true; // How to get this?
            bool patchEntryHeader    = false;

            WriteLEInt(ZipConstants.LocalHeaderSignature);

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

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

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

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

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

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

            ZipExtraData 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();

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

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

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

            if (extra.Length > 0)
            {
                stream_.Write(extra, 0, extra.Length);
            }
        }
Example #15
0
        /// <summary>
        /// Finishes the stream.  This will write the central directory at the
        /// end of the zip file and flush the stream.
        /// </summary>
        /// <exception cref="System.IO.IOException">
        /// if an I/O error occured.
        /// </exception>
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            int numEntries  = 0;
            int sizeEntries = 0;

            foreach (ZipEntry entry in entries)
            {
                // TODO : check the appnote file for compilance with the central directory standard
                CompressionMethod method = entry.CompressionMethod;
                WriteLeInt(ZipConstants.CENSIG);
                WriteLeShort(method == CompressionMethod.Stored ? ZIP_STORED_VERSION : ZIP_DEFLATED_VERSION);
                WriteLeShort(method == CompressionMethod.Stored ? ZIP_STORED_VERSION : ZIP_DEFLATED_VERSION);

                WriteLeShort(entry.Flags);
                WriteLeShort((short)method);
                WriteLeInt((int)entry.DosTime);
                WriteLeInt((int)entry.Crc);
                WriteLeInt((int)entry.CompressedSize);
                WriteLeInt((int)entry.Size);

                byte[] name = ZipConstants.ConvertToArray(entry.Name);

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

                byte[] extra = entry.ExtraData;
                if (extra == null)
                {
                    extra = new byte[0];
                }

                string strComment = entry.Comment;
                byte[] comment    = strComment != null?ZipConstants.ConvertToArray(strComment) : new byte[0];

                if (comment.Length > 0xffff)
                {
                    throw new ZipException("Comment too long.");
                }

                WriteLeShort(name.Length);
                WriteLeShort(extra.Length);
                WriteLeShort(comment.Length);
                WriteLeShort(0);                 // disk number
                WriteLeShort(0);                 // internal file attr
                if (entry.IsDirectory)           // -jr- 17-12-2003 mark entry as directory (from nikolam.AT.perfectinfo.com)
                {
                    WriteLeInt(16);
                }
                else
                {
                    WriteLeInt(0);                       // external file attr
                }
                WriteLeInt(entry.Offset);

                baseOutputStream.Write(name, 0, name.Length);
                baseOutputStream.Write(extra, 0, extra.Length);
                baseOutputStream.Write(comment, 0, comment.Length);
                ++numEntries;
                sizeEntries += ZipConstants.CENHDR + name.Length + extra.Length + comment.Length;
            }

            WriteLeInt(ZipConstants.ENDSIG);
            WriteLeShort(0);             // disk number
            WriteLeShort(0);             // disk with start of central dir
            WriteLeShort(numEntries);
            WriteLeShort(numEntries);
            WriteLeInt(sizeEntries);
            WriteLeInt(offset);
            WriteLeShort(zipComment.Length);
            baseOutputStream.Write(zipComment, 0, zipComment.Length);
            baseOutputStream.Flush();
            entries = null;
        }
Example #16
0
        /// <summary>
        /// Perform the initial read on an entry which may include
        /// reading encryption headers and setting up inflation.
        /// </summary>
        /// <param name="destination">The destination to fill with data read.</param>
        /// <param name="offset">The offset to start reading at.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The actual number of bytes read.</returns>
        int InitialRead(byte[] destination, int offset, int count)
        {
            if (!CanDecompressEntry)
            {
                throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version.ToString() + ")");
            }

            // Handle encryption if required.
            if (entry.IsCrypted)
            {
#if NETCF_1_0
                throw new ZipException("Encryption not supported for Compact Framework 1.0");
#else
                if (password == null)
                {
                    throw new ZipException("No password set.");
                }

                // Generate and set crypto transform...
                PkzipClassicManaged managed = new PkzipClassicManaged();
                byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));

                inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);

                byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
                inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);

                if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue)
                {
                    throw new ZipException("Invalid password");
                }

                if (csize >= ZipConstants.CryptoHeaderSize)
                {
                    csize -= ZipConstants.CryptoHeaderSize;
                }
                else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0)
                {
                    throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
                }
#endif
            }
            else
            {
#if !NETCF_1_0
                inputBuffer.CryptoTransform = null;
#endif
            }

            if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0))
            {
                if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0))
                {
                    inputBuffer.SetInflaterInput(inf);
                }

                internalReader = new ReadDataHandler(BodyRead);
                return(BodyRead(destination, offset, count));
            }
            else
            {
                internalReader = new ReadDataHandler(ReadingNotAvailable);
                return(0);
            }
        }
Example #17
0
        private void WriteLocalHeader(ZipEntry entry, EntryPatchData patchData)
        {
            CompressionMethod compressionMethod = entry.CompressionMethod;
            bool flag  = true;
            bool flag2 = false;

            WriteLEInt(67324752);
            WriteLEShort(entry.Version);
            WriteLEShort(entry.Flags);
            WriteLEShort((byte)compressionMethod);
            WriteLEInt((int)entry.DosTime);
            if (flag)
            {
                WriteLEInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLEInt(-1);
                    WriteLEInt(-1);
                }
                else
                {
                    WriteLEInt((int)(entry.IsCrypted ? ((int)entry.CompressedSize + 12) : entry.CompressedSize));
                    WriteLEInt((int)entry.Size);
                }
            }
            else
            {
                if (patchData != null)
                {
                    patchData.CrcPatchOffset = stream_.get_Position();
                }
                WriteLEInt(0);
                if (patchData != null)
                {
                    patchData.SizePatchOffset = stream_.get_Position();
                }
                if (entry.LocalHeaderRequiresZip64 && flag2)
                {
                    WriteLEInt(-1);
                    WriteLEInt(-1);
                }
                else
                {
                    WriteLEInt(0);
                    WriteLEInt(0);
                }
            }
            byte[] array = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (array.Length > 65535)
            {
                throw new ZipException("Entry name too long.");
            }
            ZipExtraData zipExtraData = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64 && (flag || flag2))
            {
                zipExtraData.StartNewEntry();
                if (flag)
                {
                    zipExtraData.AddLeLong(entry.Size);
                    zipExtraData.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    zipExtraData.AddLeLong(-1L);
                    zipExtraData.AddLeLong(-1L);
                }
                zipExtraData.AddNewEntry(1);
                if (!zipExtraData.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }
                if (patchData != null)
                {
                    patchData.SizePatchOffset = zipExtraData.CurrentReadIndex;
                }
            }
            else
            {
                zipExtraData.Delete(1);
            }
            byte[] entryData = zipExtraData.GetEntryData();
            WriteLEShort(array.Length);
            WriteLEShort(entryData.Length);
            if (array.Length > 0)
            {
                stream_.Write(array, 0, array.Length);
            }
            if (entry.LocalHeaderRequiresZip64 && flag2)
            {
                patchData.SizePatchOffset += stream_.get_Position();
            }
            if (entryData.Length > 0)
            {
                stream_.Write(entryData, 0, entryData.Length);
            }
        }
        /// <summary>
        /// Finishes the stream.  This will write the central directory at the
        /// end of the zip file and flush the stream.
        /// </summary>
        /// <remarks>
        /// This is automatically called when the stream is closed.
        /// </remarks>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="ZipException">
        /// Comment exceeds the maximum length<br/>
        /// Entry name exceeds the maximum length
        /// </exception>
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            long numEntries  = entries.Count;
            long sizeEntries = 0;

            foreach (ZipEntryM entry in entries)
            {
                WriteLeInt(ZipConstants.CentralHeaderSignature);
                WriteLeShort(ZipConstants.VersionMadeBy);
                WriteLeShort(entry.Version);
                WriteLeShort(entry.Flags);
                WriteLeShort((short)entry.CompressionMethodForHeader);
                WriteLeInt((int)entry.DosTime);
                WriteLeInt((int)entry.Crc);

                if (entry.IsZip64Forced() ||
                    (entry.CompressedSize >= uint.MaxValue))
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)entry.CompressedSize);
                }

                if (entry.IsZip64Forced() ||
                    (entry.Size >= uint.MaxValue))
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)entry.Size);
                }

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

                if (name.Length > 0xffff)
                {
                    //throw new ZipException("Name too long.");
                }

                var ed = new ZipExtraData(entry.ExtraData);

                if (entry.CentralHeaderRequiresZip64)
                {
                    ed.StartNewEntry();
                    if (entry.IsZip64Forced() ||
                        (entry.Size >= 0xffffffff))
                    {
                        ed.AddLeLong(entry.Size);
                    }

                    if (entry.IsZip64Forced() ||
                        (entry.CompressedSize >= 0xffffffff))
                    {
                        ed.AddLeLong(entry.CompressedSize);
                    }

                    if (entry.Offset >= 0xffffffff)
                    {
                        ed.AddLeLong(entry.Offset);
                    }

                    ed.AddNewEntry(1);
                }
                else
                {
                    ed.Delete(1);
                }

                if (entry.AESKeySize > 0)
                {
                    AddExtraDataAES(entry, ed);
                }
                byte[] extra = ed.GetEntryData();

                byte[] entryComment =
                    (entry.Comment != null) ?
                    ZipConstants.ConvertToArray(entry.Flags, entry.Comment) :
                    new byte[0];

                if (entryComment.Length > 0xffff)
                {
                    //throw new ZipException("Comment too long.");
                }

                WriteLeShort(name.Length);
                WriteLeShort(extra.Length);
                WriteLeShort(entryComment.Length);
                WriteLeShort(0);                    // disk number
                WriteLeShort(0);                    // internal file attributes
                // external file attributes

                if (entry.ExternalFileAttributes != -1)
                {
                    WriteLeInt(entry.ExternalFileAttributes);
                }
                else
                {
                    if (entry.IsDirectory)                                               // mark entry as directory (from nikolam.AT.perfectinfo.com)
                    {
                        WriteLeInt(16);
                    }
                    else
                    {
                        WriteLeInt(0);
                    }
                }

                if (entry.Offset >= uint.MaxValue)
                {
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt((int)entry.Offset);
                }

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

                if (extra.Length > 0)
                {
                    baseOutputStream_.Write(extra, 0, extra.Length);
                }

                if (entryComment.Length > 0)
                {
                    baseOutputStream_.Write(entryComment, 0, entryComment.Length);
                }

                sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length;
            }

            using (ZipHelperStream zhs = new ZipHelperStream(baseOutputStream_)) {
                zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment);
            }

            entries = null;
        }
Example #19
0
 public override void Finish()
 {
     if (this.entries != null)
     {
         if (this.curEntry != null)
         {
             this.CloseEntry();
         }
         long count       = this.entries.Count;
         long sizeEntries = 0L;
         foreach (ZipEntry entry in this.entries)
         {
             this.WriteLeInt(0x2014b50);
             this.WriteLeShort(0x33);
             this.WriteLeShort(entry.Version);
             this.WriteLeShort(entry.Flags);
             this.WriteLeShort((short)entry.CompressionMethodForHeader);
             this.WriteLeInt((int)entry.DosTime);
             this.WriteLeInt((int)entry.Crc);
             if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int)entry.CompressedSize);
             }
             if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int)entry.Size);
             }
             byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
             if (buffer.Length > 0xffff)
             {
                 throw new ZipException("Name too long.");
             }
             ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
             if (entry.CentralHeaderRequiresZip64)
             {
                 extraData.StartNewEntry();
                 if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
                 {
                     extraData.AddLeLong(entry.Size);
                 }
                 if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
                 {
                     extraData.AddLeLong(entry.CompressedSize);
                 }
                 if (entry.Offset >= 0xffffffffL)
                 {
                     extraData.AddLeLong(entry.Offset);
                 }
                 extraData.AddNewEntry(1);
             }
             else
             {
                 extraData.Delete(1);
             }
             if (entry.AESKeySize > 0)
             {
                 AddExtraDataAES(entry, extraData);
             }
             byte[] entryData = extraData.GetEntryData();
             byte[] buffer3   = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
             if (buffer3.Length > 0xffff)
             {
                 throw new ZipException("Comment too long.");
             }
             this.WriteLeShort(buffer.Length);
             this.WriteLeShort(entryData.Length);
             this.WriteLeShort(buffer3.Length);
             this.WriteLeShort(0);
             this.WriteLeShort(0);
             if (entry.ExternalFileAttributes != -1)
             {
                 this.WriteLeInt(entry.ExternalFileAttributes);
             }
             else if (entry.IsDirectory)
             {
                 this.WriteLeInt(0x10);
             }
             else
             {
                 this.WriteLeInt(0);
             }
             if (entry.Offset >= 0xffffffffL)
             {
                 this.WriteLeInt(-1);
             }
             else
             {
                 this.WriteLeInt((int)entry.Offset);
             }
             if (buffer.Length > 0)
             {
                 base.baseOutputStream_.Write(buffer, 0, buffer.Length);
             }
             if (entryData.Length > 0)
             {
                 base.baseOutputStream_.Write(entryData, 0, entryData.Length);
             }
             if (buffer3.Length > 0)
             {
                 base.baseOutputStream_.Write(buffer3, 0, buffer3.Length);
             }
             sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.Length;
         }
         using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_))
         {
             stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment);
         }
         this.entries = null;
     }
 }
Example #20
0
        /// <summary>
        /// Finishes the stream.  This will write the central directory at the
        /// end of the zip file and flush the stream.
        /// </summary>
        /// <remarks>
        /// This is automatically called when the stream is closed.
        /// </remarks>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="ZipException">
        /// Comment exceeds the maximum length<br/>
        /// Entry name exceeds the maximum length
        /// </exception>
        public override void Finish()
        {
            if (entries == null)
            {
                return;
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            int numEntries  = 0;
            int sizeEntries = 0;

            foreach (ZipEntry entry in entries)
            {
                CompressionMethod method = entry.CompressionMethod;
                WriteLeInt(ZipConstants.CENSIG);
                WriteLeShort(ZipConstants.VERSION_MADE_BY);
                WriteLeShort(entry.Version);
                WriteLeShort(entry.Flags);
                WriteLeShort((short)method);
                WriteLeInt((int)entry.DosTime);
                WriteLeInt((int)entry.Crc);
                WriteLeInt((int)entry.CompressedSize);
                WriteLeInt((int)entry.Size);

                byte[] name = ZipConstants.ConvertToArray(entry.Name);

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

                byte[] extra = entry.ExtraData;
                if (extra == null)
                {
                    extra = new byte[0];
                }

                byte[] entryComment = entry.Comment != null?ZipConstants.ConvertToArray(entry.Comment) : new byte[0];

                if (entryComment.Length > 0xffff)
                {
                    throw new ZipException("Comment too long.");
                }

                WriteLeShort(name.Length);
                WriteLeShort(extra.Length);
                WriteLeShort(entryComment.Length);
                WriteLeShort(0);                        // disk number
                WriteLeShort(0);                        // internal file attr
                // external file attribute

                if (entry.ExternalFileAttributes != -1)
                {
                    WriteLeInt(entry.ExternalFileAttributes);
                }
                else
                {
                    if (entry.IsDirectory)                                               // mark entry as directory (from nikolam.AT.perfectinfo.com)
                    {
                        WriteLeInt(16);
                    }
                    else
                    {
                        WriteLeInt(0);
                    }
                }

                WriteLeInt(entry.Offset);

                baseOutputStream.Write(name, 0, name.Length);
                baseOutputStream.Write(extra, 0, extra.Length);
                baseOutputStream.Write(entryComment, 0, entryComment.Length);
                ++numEntries;
                sizeEntries += ZipConstants.CENHDR + name.Length + extra.Length + entryComment.Length;
            }

            WriteLeInt(ZipConstants.ENDSIG);
            WriteLeShort(0);                                // number of this disk
            WriteLeShort(0);                                // no of disk with start of central dir
            WriteLeShort(numEntries);                       // entries in central dir for this disk
            WriteLeShort(numEntries);                       // total entries in central directory
            WriteLeInt(sizeEntries);                        // size of the central directory
            WriteLeInt((int)offset);                        // offset of start of central dir
            WriteLeShort(zipComment.Length);
            baseOutputStream.Write(zipComment, 0, zipComment.Length);
            baseOutputStream.Flush();
            entries = null;
        }
Example #21
0
 public override void Finish()
 {
     if (entries != null)
     {
         if (curEntry != null)
         {
             CloseEntry();
         }
         long noOfEntries = entries.Count;
         long num         = 0L;
         foreach (ZipEntry entry in entries)
         {
             WriteLeInt(33639248);
             WriteLeShort(51);
             WriteLeShort(entry.Version);
             WriteLeShort(entry.Flags);
             WriteLeShort((short)entry.CompressionMethodForHeader);
             WriteLeInt((int)entry.DosTime);
             WriteLeInt((int)entry.Crc);
             if (entry.IsZip64Forced() || entry.CompressedSize >= uint.MaxValue)
             {
                 WriteLeInt(-1);
             }
             else
             {
                 WriteLeInt((int)entry.CompressedSize);
             }
             if (entry.IsZip64Forced() || entry.Size >= uint.MaxValue)
             {
                 WriteLeInt(-1);
             }
             else
             {
                 WriteLeInt((int)entry.Size);
             }
             byte[] array = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
             if (array.Length > 65535)
             {
                 throw new ZipException("Name too long.");
             }
             ZipExtraData zipExtraData = new ZipExtraData(entry.ExtraData);
             if (entry.CentralHeaderRequiresZip64)
             {
                 zipExtraData.StartNewEntry();
                 if (entry.IsZip64Forced() || entry.Size >= uint.MaxValue)
                 {
                     zipExtraData.AddLeLong(entry.Size);
                 }
                 if (entry.IsZip64Forced() || entry.CompressedSize >= uint.MaxValue)
                 {
                     zipExtraData.AddLeLong(entry.CompressedSize);
                 }
                 if (entry.Offset >= uint.MaxValue)
                 {
                     zipExtraData.AddLeLong(entry.Offset);
                 }
                 zipExtraData.AddNewEntry(1);
             }
             else
             {
                 zipExtraData.Delete(1);
             }
             if (entry.AESKeySize > 0)
             {
                 AddExtraDataAES(entry, zipExtraData);
             }
             byte[] entryData = zipExtraData.GetEntryData();
             byte[] array2    = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
             if (array2.Length > 65535)
             {
                 throw new ZipException("Comment too long.");
             }
             WriteLeShort(array.Length);
             WriteLeShort(entryData.Length);
             WriteLeShort(array2.Length);
             WriteLeShort(0);
             WriteLeShort(0);
             if (entry.ExternalFileAttributes != -1)
             {
                 WriteLeInt(entry.ExternalFileAttributes);
             }
             else if (entry.IsDirectory)
             {
                 WriteLeInt(16);
             }
             else
             {
                 WriteLeInt(0);
             }
             if (entry.Offset >= uint.MaxValue)
             {
                 WriteLeInt(-1);
             }
             else
             {
                 WriteLeInt((int)entry.Offset);
             }
             if (array.Length != 0)
             {
                 baseOutputStream_.Write(array, 0, array.Length);
             }
             if (entryData.Length != 0)
             {
                 baseOutputStream_.Write(entryData, 0, entryData.Length);
             }
             if (array2.Length != 0)
             {
                 baseOutputStream_.Write(array2, 0, array2.Length);
             }
             num += 46 + array.Length + entryData.Length + array2.Length;
         }
         using (ZipHelperStream zipHelperStream = new ZipHelperStream(baseOutputStream_))
         {
             zipHelperStream.WriteEndOfCentralDirectory(noOfEntries, num, offset, zipComment);
         }
         entries = null;
     }
 }