public ZipOutputStream(Stream baseOutputStream)
     : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true))
 {
     this.entries = new ArrayList();
     this.crc = new Crc32();
     this.curEntry = null;
     this.offset = 0;
     this.zipComment = new byte[0];
     this.defaultMethod = 8;
     this.shouldWriteBack = false;
     this.seekPos = -1;
 }
Exemple #2
0
 public ZipEntry(ZipEntry e)
 {
     this.cal = System.DateTime.Now;
     this.known = 0;
     this.method = Framework.Helpers.ZipLib.CompressionMethod.Deflated;
     this.extra = null;
     this.comment = null;
     this.zipFileIndex = -1;
     this.name = e.name;
     this.known = e.known;
     this.size = e.size;
     this.compressedSize = e.compressedSize;
     this.crc = e.crc;
     this.method = e.method;
     this.extra = e.extra;
     this.comment = e.comment;
 }
Exemple #3
0
 public Stream GetInputStream(ZipEntry entry)
 {
     if (this.entries == null)
     {
         throw new InvalidOperationException("ZipFile has closed");
     }
     int zipFileIndex = entry.zipFileIndex;
     if (((zipFileIndex < 0) || (zipFileIndex >= this.entries.Length)) || (this.entries[zipFileIndex].Name != entry.Name))
     {
         zipFileIndex = this.GetEntryIndex(entry.Name);
         if (zipFileIndex < 0)
         {
             throw new IndexOutOfRangeException();
         }
     }
     long start = this.CheckLocalHeader(this.entries[zipFileIndex]);
     CompressionMethod compressionMethod = this.entries[zipFileIndex].CompressionMethod;
     Stream baseInputStream = new PartialInputStream(this.baseStream, start, this.entries[zipFileIndex].CompressedSize);
     CompressionMethod method2 = compressionMethod;
     if (method2 != CompressionMethod.Stored)
     {
         if (method2 != CompressionMethod.Deflated)
         {
             throw new ZipException("Unknown compression method " + compressionMethod);
         }
     }
     else
     {
         return baseInputStream;
     }
     return new InflaterInputStream(baseInputStream, new Inflater(true));
 }
Exemple #4
0
 public ZipEntryEnumeration(ZipEntry[] arr)
 {
     this.array = arr;
 }
Exemple #5
0
 private void ReadEntries()
 {
     long offset = this.baseStream.Length - 0x16;
     do
     {
         if (offset < 0)
         {
             throw new ZipException("central directory not found, probably not a zip file");
         }
         offset--;
         this.baseStream.Seek(offset, SeekOrigin.Begin);
     }
     while (this.ReadLeInt() != 0x6054b50);
     long position = this.baseStream.Position;
     this.baseStream.Position += 6;
     if ((this.baseStream.Position - position) != 6)
     {
         throw new EndOfStreamException();
     }
     int num3 = this.ReadLeShort();
     position = this.baseStream.Position;
     this.baseStream.Position += 4;
     if ((this.baseStream.Position - position) != 4)
     {
         throw new EndOfStreamException();
     }
     int num4 = this.ReadLeInt();
     this.entries = new ZipEntry[num3];
     this.baseStream.Seek((long) num4, SeekOrigin.Begin);
     for (int i = 0; i < num3; i++)
     {
         if (this.ReadLeInt() != 0x2014b50)
         {
             throw new ZipException("Wrong Central Directory signature");
         }
         position = this.baseStream.Position;
         this.baseStream.Position += 6;
         if ((this.baseStream.Position - position) != 6)
         {
             throw new EndOfStreamException();
         }
         int num6 = this.ReadLeShort();
         int num7 = this.ReadLeInt();
         int num8 = this.ReadLeInt();
         int num9 = this.ReadLeInt();
         int num10 = this.ReadLeInt();
         int num11 = this.ReadLeShort();
         int count = this.ReadLeShort();
         int num13 = this.ReadLeShort();
         position = this.baseStream.Position;
         this.baseStream.Position += 8;
         if ((this.baseStream.Position - position) != 8)
         {
             throw new EndOfStreamException();
         }
         int num14 = this.ReadLeInt();
         byte[] buffer = new byte[Math.Max(num11, num13)];
         this.baseStream.Read(buffer, 0, num11);
         ZipEntry entry = new ZipEntry(ZipConstants.ConvertToString(buffer));
         entry.CompressionMethod = (CompressionMethod) num6;
         entry.Crc = num8 & ((long) 0xffffffffL);
         entry.Size = num10 & ((long) 0xffffffffL);
         entry.CompressedSize = num9 & ((long) 0xffffffffL);
         entry.DosTime = num7;
         if (count > 0)
         {
             byte[] buffer2 = new byte[count];
             this.baseStream.Read(buffer2, 0, count);
             entry.ExtraData = buffer2;
         }
         if (num13 > 0)
         {
             this.baseStream.Read(buffer, 0, num13);
             entry.Comment = ZipConstants.ConvertToString(buffer);
         }
         entry.zipFileIndex = i;
         entry.offset = num14;
         this.entries[i] = entry;
     }
 }
Exemple #6
0
 private long CheckLocalHeader(ZipEntry entry)
 {
     lock (this.baseStream)
     {
         this.baseStream.Seek((long) entry.offset, SeekOrigin.Begin);
         if (this.ReadLeInt() != 0x4034b50)
         {
             throw new ZipException("Wrong Local header signature");
         }
         long position = this.baseStream.Position;
         this.baseStream.Position += 4;
         if ((this.baseStream.Position - position) != 4)
         {
             throw new EndOfStreamException();
         }
         //if (entry.CompressionMethod != this.ReadLeShort())
         //{
         //    throw new ZipException("Compression method mismatch");
         //}
         position = this.baseStream.Position;
         this.baseStream.Position += 0x10;
         if ((this.baseStream.Position - position) != 0x10)
         {
             throw new EndOfStreamException();
         }
         if (entry.Name.Length != this.ReadLeShort())
         {
             throw new ZipException("file name length mismatch");
         }
         int num2 = entry.Name.Length + this.ReadLeShort();
         return ((entry.offset + 30) + num2);
     }
 }
 public void CloseEntry()
 {
     if (this.curEntry == null)
     {
         throw new InvalidOperationException("No open entry");
     }
     if (this.curMethod == CompressionMethod.Deflated)
     {
         base.Finish();
     }
     int num = (this.curMethod == CompressionMethod.Deflated) ? base.def.TotalOut : this.size;
     if (this.curEntry.Size < 0)
     {
         this.curEntry.Size = this.size;
     }
     else if (this.curEntry.Size != this.size)
     {
         throw new ZipException(string.Concat(new object[] { "size was ", this.size, ", but I expected ", this.curEntry.Size }));
     }
     if (this.curEntry.CompressedSize < 0)
     {
         this.curEntry.CompressedSize = num;
     }
     else if (this.curEntry.CompressedSize != num)
     {
         throw new ZipException(string.Concat(new object[] { "compressed size was ", num, ", but I expected ", this.curEntry.CompressedSize }));
     }
     if (this.curEntry.Crc < 0)
     {
         this.curEntry.Crc = this.crc.Value;
     }
     else if (this.curEntry.Crc != this.crc.Value)
     {
         throw new ZipException(string.Concat(new object[] { "crc was ", this.crc.Value, ", but I expected ", this.curEntry.Crc }));
     }
     this.offset += num;
     if ((this.curMethod == CompressionMethod.Deflated) && ((this.curEntry.flags & 8) != 0))
     {
         if (this.shouldWriteBack)
         {
             long position = base.baseOutputStream.Position;
             base.baseOutputStream.Seek(this.seekPos, SeekOrigin.Begin);
             this.WriteLeInt((int) this.curEntry.Crc);
             this.WriteLeInt((int) this.curEntry.CompressedSize);
             this.WriteLeInt((int) this.curEntry.Size);
             base.baseOutputStream.Seek(position, SeekOrigin.Begin);
             this.shouldWriteBack = false;
         }
         else
         {
             this.WriteLeInt(0x8074b50);
             this.WriteLeInt((int) this.curEntry.Crc);
             this.WriteLeInt((int) this.curEntry.CompressedSize);
             this.WriteLeInt((int) this.curEntry.Size);
             this.offset += 0x10;
         }
     }
     this.entries.Add(this.curEntry);
     this.curEntry = null;
 }
        public void PutNextEntry(ZipEntry entry)
        {
            if (this.entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }
            CompressionMethod compressionMethod = entry.CompressionMethod;
            int num = 0;
            switch (compressionMethod)
            {
                case CompressionMethod.Stored:
                    if (entry.CompressedSize >= 0)
                    {
                        if (entry.Size >= 0)
                        {
                            if (entry.Size != entry.CompressedSize)
                            {
                                throw new ZipException("Method STORED, but compressed size != size");
                            }
                        }
                        else
                        {
                            entry.Size = entry.CompressedSize;
                        }
                    }
                    else
                    {
                        entry.CompressedSize = entry.Size;
                    }
                    if (entry.Size < 0)
                    {
                        throw new ZipException("Method STORED, but size not set");
                    }
                    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))
                    {
                        num |= 8;
                    }
                    goto Label_00C7;
            }
            Label_00C7:
            if (this.curEntry != null)
            {
                this.CloseEntry();
            }
            entry.flags = num;
            entry.offset = this.offset;
            entry.CompressionMethod = compressionMethod;
            this.curMethod = compressionMethod;
            this.WriteLeInt(0x4034b50);
            this.WriteLeShort((compressionMethod == CompressionMethod.Stored) ? 10 : 20);
            if ((num & 8) == 0)
            {
                this.WriteLeShort(num);
                this.WriteLeShort((byte) compressionMethod);
                this.WriteLeInt(entry.DosTime);
                this.WriteLeInt((int) entry.Crc);
                this.WriteLeInt((int) entry.CompressedSize);
                this.WriteLeInt((int) entry.Size);
            }
            else
            {
                if (base.baseOutputStream.CanSeek)
                {
                    this.shouldWriteBack = true;
                    this.WriteLeShort((short) (num & -9));
                }
                else
                {
                    this.shouldWriteBack = false;
                    this.WriteLeShort(num);
                }
                this.WriteLeShort((byte) compressionMethod);
                this.WriteLeInt(entry.DosTime);
                this.seekPos = 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("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();
            }
            this.size = 0;
        }
 public void CloseEntry()
 {
     if (this.crc == null)
     {
         throw new InvalidOperationException("Closed.");
     }
     if (this.entry != null)
     {
         if (this.method == 8)
         {
             if ((this.flags & 8) != 0)
             {
                 byte[] buffer = new byte[0x800];
                 while (this.Read(buffer, 0, buffer.Length) > 0)
                 {
                 }
                 return;
             }
             this.csize -= base.inf.TotalIn;
             this.avail = base.inf.RemainingInput;
         }
         if ((this.avail > this.csize) && (this.csize >= 0))
         {
             this.avail -= this.csize;
         }
         else
         {
             this.csize -= this.avail;
             this.avail = 0;
             while (this.csize != 0)
             {
                 int num = (int) base.Skip(this.csize & ((long) 0xffffffffL));
                 if (num <= 0)
                 {
                     throw new ZipException("zip archive ends early.");
                 }
                 this.csize -= num;
             }
         }
         this.size = 0;
         this.crc.Reset();
         if (this.method == 8)
         {
             base.inf.Reset();
         }
         this.entry = null;
     }
 }
 public override void Close()
 {
     base.Close();
     this.crc = null;
     this.entry = null;
 }
 public ZipInputStream(Stream baseInputStream)
     : base(baseInputStream, new Inflater(true))
 {
     this.crc = new Crc32();
     this.entry = null;
 }
        public override int Read(byte[] b, int off, int len)
        {
            if (this.crc == null)
            {
                throw new InvalidOperationException("Closed.");
            }
            if (this.entry == null)
            {
                return -1;
            }
            bool flag = false;
            switch (this.method)
            {
                case 0:
                    if ((len > this.csize) && (this.csize >= 0))
                    {
                        len = (int) this.csize;
                    }
                    len = this.ReadBuf(b, off, len);
                    if (len > 0)
                    {
                        this.csize -= len;
                        this.size -= len;
                    }
                    if (this.csize == 0)
                    {
                        flag = true;
                    }
                    else if (len < 0)
                    {
                        throw new ZipException("EOF in stored block");
                    }
                    break;

                case 8:
                    len = base.Read(b, off, len);
                    if (len < 0)
                    {
                        if (!base.inf.IsFinished)
                        {
                            throw new ZipException("Inflater not finished!?");
                        }
                        this.avail = base.inf.RemainingInput;
                        if ((this.flags & 8) != 0)
                        {
                            this.ReadDataDescr();
                        }
                        if ((base.inf.TotalIn != this.csize) || (base.inf.TotalOut != this.size))
                        {
                            throw new ZipException(string.Concat(new object[] { "size mismatch: ", this.csize, ";", this.size, " <-> ", base.inf.TotalIn, ";", base.inf.TotalOut }));
                        }
                        base.inf.Reset();
                        flag = true;
                    }
                    break;
            }
            if (len > 0)
            {
                this.crc.Update(b, off, len);
            }
            if (flag)
            {
                if ((((long) this.crc.Value) & 0xffffffffL) != this.entry.Crc)
                {
                    throw new ZipException("CRC mismatch");
                }
                this.crc.Reset();
                this.entry = null;
            }
            return len;
        }
 public ZipEntry GetNextEntry()
 {
     if (this.crc == null)
     {
         throw new InvalidOperationException("Closed.");
     }
     if (this.entry != null)
     {
         this.CloseEntry();
     }
     int num = this.ReadLeInt();
     if (num == 0x2014b50)
     {
         this.Close();
         return null;
     }
     if (num != 0x4034b50)
     {
         throw new ZipException("Wrong Local header signature" + num);
     }
     short num2 = (short) this.ReadLeShort();
     this.flags = this.ReadLeShort();
     this.method = this.ReadLeShort();
     int num3 = this.ReadLeInt();
     int num4 = this.ReadLeInt();
     this.csize = this.ReadLeInt();
     this.size = this.ReadLeInt();
     int num5 = this.ReadLeShort();
     int num6 = this.ReadLeShort();
     if ((this.method == 0) && (this.csize != this.size))
     {
         throw new ZipException("Stored, but compressed != uncompressed");
     }
     byte[] outBuf = new byte[num5];
     this.ReadFully(outBuf);
     string name = ZipConstants.ConvertToString(outBuf);
     this.entry = new ZipEntry(name);
     this.entry.Version = (ushort) num2;
     if ((this.method != 0) && (this.method != 8))
     {
         throw new ZipException("unknown compression method " + this.method);
     }
     this.entry.CompressionMethod = (CompressionMethod) this.method;
     if ((this.flags & 8) == 0)
     {
         this.entry.Crc = num4 & ((long) 0xffffffffL);
         this.entry.Size = this.size & ((long) 0xffffffffL);
         this.entry.CompressedSize = this.csize & ((long) 0xffffffffL);
     }
     this.entry.DosTime = num3;
     if (num6 > 0)
     {
         byte[] buffer2 = new byte[num6];
         this.ReadFully(buffer2);
         this.entry.ExtraData = buffer2;
     }
     if ((this.method == 8) && (this.avail > 0))
     {
         Array.Copy(base.buf, base.len - ((int) this.avail), base.buf, 0, (int) this.avail);
         base.len = (int) this.avail;
         this.avail = 0;
         base.inf.SetInput(base.buf, 0, base.len);
     }
     return this.entry;
 }