Ejemplo n.º 1
0
 private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
 {
     if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
     {
         strDirectory = strDirectory + Path.DirectorySeparatorChar;
     }
     Crc32 crc = new Crc32();
     string[] fileSystemEntries = Directory.GetFileSystemEntries(strDirectory);
     foreach (string str in fileSystemEntries)
     {
         if (Directory.Exists(str))
         {
             string str2 = parentPath;
             str2 = str2 + str.Substring(str.LastIndexOf(@"\") + 1) + @"\";
             ZipSetp(str, s, str2);
         }
         else
         {
             using (FileStream stream = File.OpenRead(str))
             {
                 byte[] buffer = new byte[stream.Length];
                 stream.Read(buffer, 0, buffer.Length);
                 ZipEntry entry = new ZipEntry(parentPath + str.Substring(str.LastIndexOf(@"\") + 1)) {
                     DateTime = DateTime.Now,
                     Size = stream.Length
                 };
                 stream.Close();
                 crc.Reset();
                 crc.Update(buffer);
                 entry.Crc = crc.Value;
                 s.PutNextEntry(entry);
                 s.Write(buffer, 0, buffer.Length);
             }
         }
     }
 }
Ejemplo n.º 2
0
 private bool ReadHeader()
 {
     int num4;
     int num5;
     this.crc = new Crc32();
     if (base.inputBuffer.Available <= 0)
     {
         base.inputBuffer.Fill();
         if (base.inputBuffer.Available <= 0)
         {
             return false;
         }
     }
     Crc32 crc = new Crc32();
     int num = base.inputBuffer.ReadLeByte();
     if (num < 0)
     {
         throw new EndOfStreamException("EOS reading GZIP header");
     }
     crc.Update(num);
     if (num != 0x1f)
     {
         throw new GZipException("Error GZIP header, first magic byte doesn't match");
     }
     num = base.inputBuffer.ReadLeByte();
     if (num < 0)
     {
         throw new EndOfStreamException("EOS reading GZIP header");
     }
     if (num != 0x8b)
     {
         throw new GZipException("Error GZIP header,  second magic byte doesn't match");
     }
     crc.Update(num);
     int num2 = base.inputBuffer.ReadLeByte();
     if (num2 < 0)
     {
         throw new EndOfStreamException("EOS reading GZIP header");
     }
     if (num2 != 8)
     {
         throw new GZipException("Error GZIP header, data not in deflate format");
     }
     crc.Update(num2);
     int num3 = base.inputBuffer.ReadLeByte();
     if (num3 < 0)
     {
         throw new EndOfStreamException("EOS reading GZIP header");
     }
     crc.Update(num3);
     if ((num3 & 0xe0) != 0)
     {
         throw new GZipException("Reserved flag bits in GZIP header != 0");
     }
     for (num4 = 0; num4 < 6; num4++)
     {
         num5 = base.inputBuffer.ReadLeByte();
         if (num5 < 0)
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         crc.Update(num5);
     }
     if ((num3 & 4) != 0)
     {
         for (num4 = 0; num4 < 2; num4++)
         {
             num5 = base.inputBuffer.ReadLeByte();
             if (num5 < 0)
             {
                 throw new EndOfStreamException("EOS reading GZIP header");
             }
             crc.Update(num5);
         }
         if ((base.inputBuffer.ReadLeByte() < 0) || (base.inputBuffer.ReadLeByte() < 0))
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         int num6 = base.inputBuffer.ReadLeByte();
         int num7 = base.inputBuffer.ReadLeByte();
         if ((num6 < 0) || (num7 < 0))
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         crc.Update(num6);
         crc.Update(num7);
         int num8 = (num6 << 8) | num7;
         for (num4 = 0; num4 < num8; num4++)
         {
             num5 = base.inputBuffer.ReadLeByte();
             if (num5 < 0)
             {
                 throw new EndOfStreamException("EOS reading GZIP header");
             }
             crc.Update(num5);
         }
     }
     if ((num3 & 8) != 0)
     {
         while ((num5 = base.inputBuffer.ReadLeByte()) > 0)
         {
             crc.Update(num5);
         }
         if (num5 < 0)
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         crc.Update(num5);
     }
     if ((num3 & 0x10) != 0)
     {
         while ((num5 = base.inputBuffer.ReadLeByte()) > 0)
         {
             crc.Update(num5);
         }
         if (num5 < 0)
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         crc.Update(num5);
     }
     if ((num3 & 2) != 0)
     {
         int num10 = base.inputBuffer.ReadLeByte();
         if (num10 < 0)
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         int num9 = base.inputBuffer.ReadLeByte();
         if (num9 < 0)
         {
             throw new EndOfStreamException("EOS reading GZIP header");
         }
         num10 = (num10 << 8) | num9;
         if (num10 != (((int) crc.Value) & 0xffff))
         {
             throw new GZipException("Header CRC value mismatch");
         }
     }
     this.readGZIPHeader = true;
     return true;
 }
Ejemplo n.º 3
0
 private void CopyBytes(ZipUpdate update, Stream destination, Stream source, long bytesToCopy, bool updateCrc)
 {
     int num3;
     if (destination == source)
     {
         throw new InvalidOperationException("Destination and source are the same");
     }
     Crc32 crc = new Crc32();
     byte[] buffer = this.GetBuffer();
     long num = bytesToCopy;
     long num2 = 0L;
     do
     {
         int length = buffer.Length;
         if (bytesToCopy < length)
         {
             length = (int) bytesToCopy;
         }
         num3 = source.Read(buffer, 0, length);
         if (num3 > 0)
         {
             if (updateCrc)
             {
                 crc.Update(buffer, 0, num3);
             }
             destination.Write(buffer, 0, num3);
             bytesToCopy -= num3;
             num2 += num3;
         }
     }
     while ((num3 > 0) && (bytesToCopy > 0L));
     if (num2 != num)
     {
         throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", num, num2));
     }
     if (updateCrc)
     {
         update.OutEntry.Crc = crc.Value;
     }
 }
Ejemplo n.º 4
0
 private void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref long destinationPosition, ref long sourcePosition)
 {
     int num4;
     long compressedSize = update.Entry.CompressedSize;
     Crc32 crc = new Crc32();
     byte[] buffer = this.GetBuffer();
     long num2 = compressedSize;
     long num3 = 0L;
     do
     {
         int length = buffer.Length;
         if (compressedSize < length)
         {
             length = (int) compressedSize;
         }
         stream.Position = sourcePosition;
         num4 = stream.Read(buffer, 0, length);
         if (num4 > 0)
         {
             if (updateCrc)
             {
                 crc.Update(buffer, 0, num4);
             }
             stream.Position = destinationPosition;
             stream.Write(buffer, 0, num4);
             destinationPosition += num4;
             sourcePosition += num4;
             compressedSize -= num4;
             num3 += num4;
         }
     }
     while ((num4 > 0) && (compressedSize > 0L));
     if (num3 != num2)
     {
         throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", num2, num3));
     }
     if (updateCrc)
     {
         update.OutEntry.Crc = crc.Value;
     }
 }
Ejemplo n.º 5
0
 public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
 {
     if (this.isDisposed_)
     {
         throw new ObjectDisposedException("ZipFile");
     }
     TestStatus status = new TestStatus(this);
     if (resultHandler != null)
     {
         resultHandler(status, null);
     }
     HeaderTest tests = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header;
     bool flag = true;
     try
     {
         for (int i = 0; flag && (i < this.Count); i++)
         {
             if (resultHandler != null)
             {
                 status.SetEntry(this[i]);
                 status.SetOperation(TestOperation.EntryHeader);
                 resultHandler(status, null);
             }
             try
             {
                 this.TestLocalHeader(this[i], tests);
             }
             catch (ZipException exception)
             {
                 status.AddError();
                 if (resultHandler != null)
                 {
                     resultHandler(status, string.Format("Exception during test - '{0}'", exception.Message));
                 }
                 if (strategy == TestStrategy.FindFirstError)
                 {
                     flag = false;
                 }
             }
             if ((flag && testData) && this[i].IsFile)
             {
                 if (resultHandler != null)
                 {
                     status.SetOperation(TestOperation.EntryData);
                     resultHandler(status, null);
                 }
                 Crc32 crc = new Crc32();
                 using (Stream stream = this.GetInputStream(this[i]))
                 {
                     int num3;
                     byte[] buffer = new byte[0x1000];
                     long num2 = 0L;
                     while ((num3 = stream.Read(buffer, 0, buffer.Length)) > 0)
                     {
                         crc.Update(buffer, 0, num3);
                         if (resultHandler != null)
                         {
                             num2 += num3;
                             status.SetBytesTested(num2);
                             resultHandler(status, null);
                         }
                     }
                 }
                 if (this[i].Crc != crc.Value)
                 {
                     status.AddError();
                     if (resultHandler != null)
                     {
                         resultHandler(status, "CRC mismatch");
                     }
                     if (strategy == TestStrategy.FindFirstError)
                     {
                         flag = false;
                     }
                 }
                 if ((this[i].Flags & 8) != 0)
                 {
                     ZipHelperStream stream2 = new ZipHelperStream(this.baseStream_);
                     DescriptorData data = new DescriptorData();
                     stream2.ReadDataDescriptor(this[i].LocalHeaderRequiresZip64, data);
                     if (this[i].Crc != data.Crc)
                     {
                         status.AddError();
                     }
                     if (this[i].CompressedSize != data.CompressedSize)
                     {
                         status.AddError();
                     }
                     if (this[i].Size != data.Size)
                     {
                         status.AddError();
                     }
                 }
             }
             if (resultHandler != null)
             {
                 status.SetOperation(TestOperation.EntryComplete);
                 resultHandler(status, null);
             }
         }
         if (resultHandler != null)
         {
             status.SetOperation(TestOperation.MiscellaneousTests);
             resultHandler(status, null);
         }
     }
     catch (Exception exception2)
     {
         status.AddError();
         if (resultHandler != null)
         {
             resultHandler(status, string.Format("Exception during test - '{0}'", exception2.Message));
         }
     }
     if (resultHandler != null)
     {
         status.SetOperation(TestOperation.Complete);
         status.SetEntry(null);
         resultHandler(status, null);
     }
     return (status.ErrorCount == 0);
 }