예제 #1
0
 public ZipEntry(ZipEntry entry)
 {
     this.externalFileAttributes = -1;
     this.method = Maticsoft.ZipLib.Zip.CompressionMethod.Deflated;
     this.zipFileIndex = -1L;
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     this.known = entry.known;
     this.name = entry.name;
     this.size = entry.size;
     this.compressedSize = entry.compressedSize;
     this.crc = entry.crc;
     this.dosTime = entry.dosTime;
     this.method = entry.method;
     this.comment = entry.comment;
     this.versionToExtract = entry.versionToExtract;
     this.versionMadeBy = entry.versionMadeBy;
     this.externalFileAttributes = entry.externalFileAttributes;
     this.flags = entry.flags;
     this.zipFileIndex = entry.zipFileIndex;
     this.offset = entry.offset;
     this.forceZip64_ = entry.forceZip64_;
     if (entry.extra != null)
     {
         this.extra = new byte[entry.extra.Length];
         Array.Copy(entry.extra, 0, this.extra, 0, entry.extra.Length);
     }
 }
예제 #2
0
        public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
        {
            ZipEntry entry = new ZipEntry(this.nameTransform_.TransformDirectory(directoryName)) {
                IsUnicodeText = this.isUnicodeText_,
                Size = 0L
            };
            int num = 0;
            DirectoryInfo info = null;
            if (useFileSystem)
            {
                info = new DirectoryInfo(directoryName);
            }
            if ((info == null) || !info.Exists)
            {
                if (this.timeSetting_ == TimeSetting.Fixed)
                {
                    entry.DateTime = this.fixedDateTime_;
                }
            }
            else
            {
                switch (this.timeSetting_)
                {
                    case TimeSetting.LastWriteTime:
                        entry.DateTime = info.LastWriteTime;
                        break;

                    case TimeSetting.LastWriteTimeUtc:
                        entry.DateTime = info.LastWriteTimeUtc;
                        break;

                    case TimeSetting.CreateTime:
                        entry.DateTime = info.CreationTime;
                        break;

                    case TimeSetting.CreateTimeUtc:
                        entry.DateTime = info.CreationTimeUtc;
                        break;

                    case TimeSetting.LastAccessTime:
                        entry.DateTime = info.LastAccessTime;
                        break;

                    case TimeSetting.LastAccessTimeUtc:
                        entry.DateTime = info.LastAccessTimeUtc;
                        break;

                    case TimeSetting.Fixed:
                        entry.DateTime = this.fixedDateTime_;
                        break;

                    default:
                        throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
                }
                num = ((int) info.Attributes) & this.getAttributes_;
            }
            num |= this.setAttributes_ | 0x10;
            entry.ExternalFileAttributes = num;
            return entry;
        }
예제 #3
0
 public Stream GetSource(ZipEntry entry, string name)
 {
     Stream stream = null;
     if (name != null)
     {
         stream = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
     }
     return stream;
 }
예제 #4
0
 public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
 {
     if (!File.Exists(fileToZip))
     {
         throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
     }
     using (FileStream stream = File.Create(zipedFile))
     {
         using (ZipOutputStream stream2 = new ZipOutputStream(stream))
         {
             using (FileStream stream3 = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
             {
                 ZipEntry entry = new ZipEntry(fileToZip.Substring(fileToZip.LastIndexOf(@"\") + 1));
                 stream2.PutNextEntry(entry);
                 stream2.SetLevel(compressionLevel);
                 byte[] buffer = new byte[blockSize];
                 int count = 0;
                 try
                 {
                     do
                     {
                         count = stream3.Read(buffer, 0, buffer.Length);
                         stream2.Write(buffer, 0, count);
                     }
                     while (count > 0);
                 }
                 catch (Exception exception)
                 {
                     throw exception;
                 }
                 stream3.Close();
             }
             stream2.Finish();
             stream2.Close();
         }
         stream.Close();
     }
 }
예제 #5
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);
             }
         }
     }
 }
예제 #6
0
 public ZipUpdate(ZipFile.UpdateCommand command, ZipEntry entry)
 {
     this.sizePatchOffset_ = -1L;
     this.crcPatchOffset_ = -1L;
     this._offsetBasedSize = -1L;
     this.command_ = command;
     this.entry_ = (ZipEntry) entry.Clone();
 }
예제 #7
0
 public ZipUpdate(ZipEntry entry) : this(ZipFile.UpdateCommand.Copy, entry)
 {
 }
예제 #8
0
 private void CompleteCloseEntry(bool testCrc)
 {
     base.StopDecrypting();
     if ((this.flags & 8) != 0)
     {
         this.ReadDataDescriptor();
     }
     this.size = 0L;
     if ((testCrc && ((((ulong) this.crc.Value) & 0xffffffffL) != this.entry.Crc)) && (this.entry.Crc != -1L))
     {
         throw new ZipException("CRC mismatch");
     }
     this.crc.Reset();
     if (this.method == 8)
     {
         base.inf.Reset();
     }
     this.entry = null;
 }
예제 #9
0
 internal void SetEntry(ZipEntry entry)
 {
     this.entry_ = entry;
     this.entryValid_ = true;
     this.bytesTested_ = 0L;
 }
예제 #10
0
 private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
 {
     CryptoStream stream = null;
     if ((entry.Version < 50) || ((entry.Flags & 0x40) == 0))
     {
         PkzipClassicManaged managed = new PkzipClassicManaged();
         this.OnKeysRequired(entry.Name);
         if (!this.HaveKeys)
         {
             throw new ZipException("No password available for encrypted stream");
         }
         stream = new CryptoStream(new UncompressedStream(baseStream), managed.CreateEncryptor(this.key, null), CryptoStreamMode.Write);
         if ((entry.Crc < 0L) || ((entry.Flags & 8) != 0))
         {
             WriteEncryptionHeader(stream, entry.DosTime << 0x10);
             return stream;
         }
         WriteEncryptionHeader(stream, entry.Crc);
     }
     return stream;
 }
예제 #11
0
 protected void InitializeAESPassword(ZipEntry entry, string rawPassword, out byte[] salt, out byte[] pwdVerifier)
 {
     salt = new byte[entry.AESSaltLen];
     if (_aesRnd == null)
     {
         _aesRnd = new RNGCryptoServiceProvider();
     }
     _aesRnd.GetBytes(salt);
     int blockSize = entry.AESKeySize / 8;
     this.cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
     pwdVerifier = ((ZipAESTransform) this.cryptoTransform_).PwdVerifier;
 }
예제 #12
0
 private static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry)
 {
     byte[] buffer = new byte[12];
     StreamUtils.ReadFully(classicCryptoStream, buffer);
     if (buffer[11] != entry.CryptoCheckValue)
     {
         throw new ZipException("Invalid password");
     }
 }
예제 #13
0
 private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
 {
     CryptoStream classicCryptoStream = null;
     if ((entry.Version < 50) || ((entry.Flags & 0x40) == 0))
     {
         PkzipClassicManaged managed = new PkzipClassicManaged();
         this.OnKeysRequired(entry.Name);
         if (!this.HaveKeys)
         {
             throw new ZipException("No password available for encrypted stream");
         }
         classicCryptoStream = new CryptoStream(baseStream, managed.CreateDecryptor(this.key, null), CryptoStreamMode.Read);
         CheckClassicPassword(classicCryptoStream, entry);
         return classicCryptoStream;
     }
     if (entry.Version != 0x33)
     {
         throw new ZipException("Decryption method not supported");
     }
     this.OnKeysRequired(entry.Name);
     if (!this.HaveKeys)
     {
         throw new ZipException("No password available for AES encrypted stream");
     }
     int aESSaltLen = entry.AESSaltLen;
     byte[] buffer = new byte[aESSaltLen];
     int num2 = baseStream.Read(buffer, 0, aESSaltLen);
     if (num2 != aESSaltLen)
     {
         throw new ZipException(string.Concat(new object[] { "AES Salt expected ", aESSaltLen, " got ", num2 }));
     }
     byte[] buffer2 = new byte[2];
     baseStream.Read(buffer2, 0, 2);
     int blockSize = entry.AESKeySize / 8;
     ZipAESTransform transform = new ZipAESTransform(this.rawPassword_, buffer, blockSize, false);
     byte[] pwdVerifier = transform.PwdVerifier;
     if ((pwdVerifier[0] != buffer2[0]) || (pwdVerifier[1] != buffer2[1]))
     {
         throw new Exception("Invalid password for AES");
     }
     return new ZipAESStream(baseStream, transform, CryptoStreamMode.Read);
 }
예제 #14
0
 public ZipUpdate(string fileName, string entryName, CompressionMethod compressionMethod)
 {
     this.sizePatchOffset_ = -1L;
     this.crcPatchOffset_ = -1L;
     this._offsetBasedSize = -1L;
     this.command_ = ZipFile.UpdateCommand.Add;
     this.entry_ = new ZipEntry(entryName);
     this.entry_.CompressionMethod = compressionMethod;
     this.filename_ = fileName;
 }
예제 #15
0
 public ZipUpdate(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
 {
     this.sizePatchOffset_ = -1L;
     this.crcPatchOffset_ = -1L;
     this._offsetBasedSize = -1L;
     this.command_ = ZipFile.UpdateCommand.Add;
     this.entry_ = new ZipEntry(entryName);
     this.entry_.CompressionMethod = compressionMethod;
     this.dataSource_ = dataSource;
 }
예제 #16
0
 public ZipUpdate(string fileName, ZipEntry entry)
 {
     this.sizePatchOffset_ = -1L;
     this.crcPatchOffset_ = -1L;
     this._offsetBasedSize = -1L;
     this.command_ = ZipFile.UpdateCommand.Add;
     this.entry_ = entry;
     this.filename_ = fileName;
 }
예제 #17
0
 public static void ZipFile(string fileToZip, string zipedFile)
 {
     if (!File.Exists(fileToZip))
     {
         throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
     }
     using (FileStream stream = File.OpenRead(fileToZip))
     {
         byte[] buffer = new byte[stream.Length];
         stream.Read(buffer, 0, buffer.Length);
         stream.Close();
         using (FileStream stream2 = File.Create(zipedFile))
         {
             using (ZipOutputStream stream3 = new ZipOutputStream(stream2))
             {
                 ZipEntry entry = new ZipEntry(fileToZip.Substring(fileToZip.LastIndexOf(@"\") + 1));
                 stream3.PutNextEntry(entry);
                 stream3.SetLevel(5);
                 stream3.Write(buffer, 0, buffer.Length);
                 stream3.Finish();
                 stream3.Close();
             }
         }
     }
 }
예제 #18
0
 public void Delete(ZipEntry entry)
 {
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     this.CheckUpdating();
     int num = this.FindExistingUpdate(entry);
     if (num < 0)
     {
         throw new ZipException("Cannot find entry to delete");
     }
     this.contentsEdited_ = true;
     this.updates_[num] = null;
     this.updateCount_ -= 1L;
 }
예제 #19
0
 public ZipUpdate(IStaticDataSource dataSource, ZipEntry entry)
 {
     this.sizePatchOffset_ = -1L;
     this.crcPatchOffset_ = -1L;
     this._offsetBasedSize = -1L;
     this.command_ = ZipFile.UpdateCommand.Add;
     this.entry_ = entry;
     this.dataSource_ = dataSource;
 }
예제 #20
0
 private int FindExistingUpdate(ZipEntry entry)
 {
     int num = -1;
     string transformedFileName = this.GetTransformedFileName(entry.Name);
     if (this.updateIndex_.ContainsKey(transformedFileName))
     {
         num = (int) this.updateIndex_[transformedFileName];
     }
     return num;
 }
예제 #21
0
 public override void Close()
 {
     this.internalReader = new ReadDataHandler(this.ReadingNotAvailable);
     this.crc = null;
     this.entry = null;
     base.Close();
 }
예제 #22
0
        public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
        {
            ZipEntry entry = new ZipEntry(this.nameTransform_.TransformFile(fileName)) {
                IsUnicodeText = this.isUnicodeText_
            };
            int num = 0;
            bool flag = this.setAttributes_ != 0;
            FileInfo info = null;
            if (useFileSystem)
            {
                info = new FileInfo(fileName);
            }
            if ((info == null) || !info.Exists)
            {
                if (this.timeSetting_ == TimeSetting.Fixed)
                {
                    entry.DateTime = this.fixedDateTime_;
                }
            }
            else
            {
                switch (this.timeSetting_)
                {
                    case TimeSetting.LastWriteTime:
                        entry.DateTime = info.LastWriteTime;
                        break;

                    case TimeSetting.LastWriteTimeUtc:
                        entry.DateTime = info.LastWriteTimeUtc;
                        break;

                    case TimeSetting.CreateTime:
                        entry.DateTime = info.CreationTime;
                        break;

                    case TimeSetting.CreateTimeUtc:
                        entry.DateTime = info.CreationTimeUtc;
                        break;

                    case TimeSetting.LastAccessTime:
                        entry.DateTime = info.LastAccessTime;
                        break;

                    case TimeSetting.LastAccessTimeUtc:
                        entry.DateTime = info.LastAccessTimeUtc;
                        break;

                    case TimeSetting.Fixed:
                        entry.DateTime = this.fixedDateTime_;
                        break;

                    default:
                        throw new ZipException("Unhandled time setting in MakeFileEntry");
                }
                entry.Size = info.Length;
                flag = true;
                num = ((int) info.Attributes) & this.getAttributes_;
            }
            if (flag)
            {
                num |= this.setAttributes_;
                entry.ExternalFileAttributes = num;
            }
            return entry;
        }
예제 #23
0
        public ZipEntry GetNextEntry()
        {
            if (this.crc == null)
            {
                throw new InvalidOperationException("Closed.");
            }
            if (this.entry != null)
            {
                this.CloseEntry();
            }
            int num = base.inputBuffer.ReadLeInt();
            switch (num)
            {
                case 0x2014b50:
                case 0x6054b50:
                case 0x5054b50:
                case 0x7064b50:
                case 0x6064b50:
                    this.Close();
                    return null;

                case 0x30304b50:
                case 0x8074b50:
                    num = base.inputBuffer.ReadLeInt();
                    break;
            }
            if (num != 0x4034b50)
            {
                throw new ZipException("Wrong Local header signature: 0x" + string.Format("{0:X}", num));
            }
            short versionRequiredToExtract = (short) base.inputBuffer.ReadLeShort();
            this.flags = base.inputBuffer.ReadLeShort();
            this.method = base.inputBuffer.ReadLeShort();
            uint num3 = (uint) base.inputBuffer.ReadLeInt();
            int num4 = base.inputBuffer.ReadLeInt();
            base.csize = base.inputBuffer.ReadLeInt();
            this.size = base.inputBuffer.ReadLeInt();
            int num5 = base.inputBuffer.ReadLeShort();
            int num6 = base.inputBuffer.ReadLeShort();
            bool flag = (this.flags & 1) == 1;
            byte[] buffer = new byte[num5];
            base.inputBuffer.ReadRawBuffer(buffer);
            string name = ZipConstants.ConvertToStringExt(this.flags, buffer);
            this.entry = new ZipEntry(name, versionRequiredToExtract);
            this.entry.Flags = this.flags;
            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 = base.csize & ((long) 0xffffffffL);
                this.entry.CryptoCheckValue = (byte) ((num4 >> 0x18) & 0xff);
            }
            else
            {
                if (num4 != 0)
                {
                    this.entry.Crc = num4 & ((long) 0xffffffffL);
                }
                if (this.size != 0L)
                {
                    this.entry.Size = this.size & ((long) 0xffffffffL);
                }
                if (base.csize != 0L)
                {
                    this.entry.CompressedSize = base.csize & ((long) 0xffffffffL);
                }
                this.entry.CryptoCheckValue = (byte) ((num3 >> 8) & 0xff);
            }
            this.entry.DosTime = num3;
            if (num6 > 0)
            {
                byte[] buffer2 = new byte[num6];
                base.inputBuffer.ReadRawBuffer(buffer2);
                this.entry.ExtraData = buffer2;
            }
            this.entry.ProcessExtraData(true);
            if (this.entry.CompressedSize >= 0L)
            {
                base.csize = this.entry.CompressedSize;
            }
            if (this.entry.Size >= 0L)
            {
                this.size = this.entry.Size;
            }
            if ((this.method == 0) && ((!flag && (base.csize != this.size)) || (flag && ((base.csize - 12L) != this.size))))
            {
                throw new ZipException("Stored, but compressed != uncompressed");
            }
            if (this.entry.IsCompressionMethodSupported())
            {
                this.internalReader = new ReadDataHandler(this.InitialRead);
            }
            else
            {
                this.internalReader = new ReadDataHandler(this.ReadingNotSupported);
            }
            return this.entry;
        }
예제 #24
0
 public int WriteDataDescriptor(ZipEntry entry)
 {
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     int num = 0;
     if ((entry.Flags & 8) == 0)
     {
         return num;
     }
     this.WriteLEInt(0x8074b50);
     this.WriteLEInt((int) entry.Crc);
     num += 8;
     if (entry.LocalHeaderRequiresZip64)
     {
         this.WriteLELong(entry.CompressedSize);
         this.WriteLELong(entry.Size);
         return (num + 0x10);
     }
     this.WriteLEInt((int) entry.CompressedSize);
     this.WriteLEInt((int) entry.Size);
     return (num + 8);
 }
예제 #25
0
 public Stream GetInputStream(ZipEntry entry)
 {
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     if (this.isDisposed_)
     {
         throw new ObjectDisposedException("ZipFile");
     }
     long zipFileIndex = entry.ZipFileIndex;
     if (((zipFileIndex < 0L) || (zipFileIndex >= this.entries_.Length)) || (this.entries_[(int) ((IntPtr) zipFileIndex)].Name != entry.Name))
     {
         zipFileIndex = this.FindEntry(entry.Name, true);
         if (zipFileIndex < 0L)
         {
             throw new ZipException("Entry cannot be found");
         }
     }
     return this.GetInputStream(zipFileIndex);
 }
예제 #26
0
 public ZipUpdate(ZipEntry original, ZipEntry updated)
 {
     this.sizePatchOffset_ = -1L;
     this.crcPatchOffset_ = -1L;
     this._offsetBasedSize = -1L;
     throw new ZipException("Modify not currently supported");
 }
예제 #27
0
 private Stream GetOutputStream(ZipEntry entry)
 {
     Stream baseStream = this.baseStream_;
     if (entry.IsCrypted)
     {
         baseStream = this.CreateAndInitEncryptionStream(baseStream, entry);
     }
     CompressionMethod compressionMethod = entry.CompressionMethod;
     if (compressionMethod != CompressionMethod.Stored)
     {
         if (compressionMethod != CompressionMethod.Deflated)
         {
             throw new ZipException("Unknown compression method " + entry.CompressionMethod);
         }
     }
     else
     {
         return new UncompressedStream(baseStream);
     }
     return new DeflaterOutputStream(baseStream, new Deflater(9, true)) { IsStreamOwner = false };
 }
예제 #28
0
 private long LocateEntry(ZipEntry entry)
 {
     return this.TestLocalHeader(entry, HeaderTest.Extract);
 }
예제 #29
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);
     }
 }
예제 #30
0
 public ZipEntryEnumerator(ZipEntry[] entries)
 {
     this.array = entries;
 }