private void WriteCentralDirRecord(ZipStorer.ZipFileEntry _zfe) { Encoding encoding = _zfe.EncodeUTF8 ? Encoding.UTF8 : ZipStorer.DefaultEncoding; byte[] bytes = encoding.GetBytes(_zfe.FilenameInZip); byte[] bytes2 = encoding.GetBytes(_zfe.Comment); this.ZipFileStream.Write(new byte[] { 80, 75, 1, 2, 23, 11, 20, 0 }, 0, 8); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.EncodeUTF8 ? 2048 : 0), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes((ushort)_zfe.Method), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(this.DateTimeToDosTime(_zfe.ModifyTime)), 0, 4); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.Crc32), 0, 4); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.CompressedSize), 0, 4); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.FileSize), 0, 4); this.ZipFileStream.Write(BitConverter.GetBytes((ushort)bytes.Length), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(0), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes((ushort)bytes2.Length), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(0), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(0), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(0), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(33024), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.HeaderOffset), 0, 4); this.ZipFileStream.Write(bytes, 0, bytes.Length); this.ZipFileStream.Write(bytes2, 0, bytes2.Length); }
private void WriteLocalHeader(ref ZipStorer.ZipFileEntry _zfe) { long position = this.ZipFileStream.Position; Encoding encoding = _zfe.EncodeUTF8 ? Encoding.UTF8 : ZipStorer.DefaultEncoding; byte[] bytes = encoding.GetBytes(_zfe.FilenameInZip); this.ZipFileStream.Write(new byte[] { 80, 75, 3, 4, 20, 0 }, 0, 6); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.EncodeUTF8 ? 2048 : 0), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes((ushort)_zfe.Method), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(this.DateTimeToDosTime(_zfe.ModifyTime)), 0, 4); Stream arg_B7_0 = this.ZipFileStream; byte[] buffer = new byte[12]; arg_B7_0.Write(buffer, 0, 12); this.ZipFileStream.Write(BitConverter.GetBytes((ushort)bytes.Length), 0, 2); this.ZipFileStream.Write(BitConverter.GetBytes(0), 0, 2); this.ZipFileStream.Write(bytes, 0, bytes.Length); _zfe.HeaderSize = (uint)(this.ZipFileStream.Position - position); }
public void AddStream(ZipStorer.Compression _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment) { if (this.Access == FileAccess.Read) { throw new InvalidOperationException("Writing is not alowed"); } if (this.Files.Count != 0) { ZipStorer.ZipFileEntry zipFileEntry = this.Files[this.Files.Count - 1]; } ZipStorer.ZipFileEntry item = default(ZipStorer.ZipFileEntry); item.Method = _method; item.EncodeUTF8 = this.EncodeUTF8; item.FilenameInZip = this.NormalizedFilename(_filenameInZip); item.Comment = ((_comment == null) ? "" : _comment); item.Crc32 = 0u; item.HeaderOffset = (uint)this.ZipFileStream.Position; item.ModifyTime = _modTime; this.WriteLocalHeader(ref item); item.FileOffset = (uint)this.ZipFileStream.Position; this.Store(ref item, _source); _source.Close(); this.UpdateCrcAndSizes(ref item); this.Files.Add(item); }
private void UpdateCrcAndSizes(ref ZipStorer.ZipFileEntry _zfe) { long position = this.ZipFileStream.Position; this.ZipFileStream.Position = (long)((ulong)(_zfe.HeaderOffset + 8u)); this.ZipFileStream.Write(BitConverter.GetBytes((ushort)_zfe.Method), 0, 2); this.ZipFileStream.Position = (long)((ulong)(_zfe.HeaderOffset + 14u)); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.Crc32), 0, 4); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.CompressedSize), 0, 4); this.ZipFileStream.Write(BitConverter.GetBytes(_zfe.FileSize), 0, 4); this.ZipFileStream.Position = position; }
private void Store(ref ZipStorer.ZipFileEntry _zfe, Stream _source) { byte[] array = new byte[16384]; uint num = 0u; long position = this.ZipFileStream.Position; long position2 = _source.Position; Stream stream; if (_zfe.Method == ZipStorer.Compression.Store) { stream = this.ZipFileStream; } else { stream = new DeflateStream(this.ZipFileStream, CompressionMode.Compress, true); } _zfe.Crc32 = 4294967295u; int num2; do { num2 = _source.Read(array, 0, array.Length); num += (uint)num2; if (num2 > 0) { stream.Write(array, 0, num2); uint num3 = 0u; while ((ulong)num3 < (ulong)((long)num2)) { _zfe.Crc32 = (ZipStorer.CrcTable[(int)((UIntPtr)((_zfe.Crc32 ^ (uint)array[(int)((UIntPtr)num3)]) & 255u))] ^ _zfe.Crc32 >> 8); num3 += 1u; } } }while (num2 == array.Length); stream.Flush(); if (_zfe.Method == ZipStorer.Compression.Deflate) { stream.Dispose(); } _zfe.Crc32 ^= 4294967295u; _zfe.FileSize = num; _zfe.CompressedSize = (uint)(this.ZipFileStream.Position - position); if (_zfe.Method == ZipStorer.Compression.Deflate && !this.ForceDeflating && _source.CanSeek && _zfe.CompressedSize > _zfe.FileSize) { _zfe.Method = ZipStorer.Compression.Store; this.ZipFileStream.Position = position; this.ZipFileStream.SetLength(position); _source.Position = position2; this.Store(ref _zfe, _source); } }
public List <ZipStorer.ZipFileEntry> ReadCentralDir() { if (this.CentralDirImage == null) { throw new InvalidOperationException("Central directory currently does not exist"); } List <ZipStorer.ZipFileEntry> list = new List <ZipStorer.ZipFileEntry>(); ushort num2; ushort num3; ushort num4; for (int i = 0; i < this.CentralDirImage.Length; i += (int)(46 + num2 + num3 + num4)) { uint num = BitConverter.ToUInt32(this.CentralDirImage, i); if (num != 33639248u) { break; } bool flag = (BitConverter.ToUInt16(this.CentralDirImage, i + 8) & 2048) != 0; ushort method = BitConverter.ToUInt16(this.CentralDirImage, i + 10); uint dt = BitConverter.ToUInt32(this.CentralDirImage, i + 12); uint crc = BitConverter.ToUInt32(this.CentralDirImage, i + 16); uint compressedSize = BitConverter.ToUInt32(this.CentralDirImage, i + 20); uint fileSize = BitConverter.ToUInt32(this.CentralDirImage, i + 24); num2 = BitConverter.ToUInt16(this.CentralDirImage, i + 28); num3 = BitConverter.ToUInt16(this.CentralDirImage, i + 30); num4 = BitConverter.ToUInt16(this.CentralDirImage, i + 32); uint headerOffset = BitConverter.ToUInt32(this.CentralDirImage, i + 42); uint headerSize = (uint)(46 + num2 + num3 + num4); Encoding encoding = flag ? Encoding.UTF8 : ZipStorer.DefaultEncoding; ZipStorer.ZipFileEntry item = default(ZipStorer.ZipFileEntry); item.Method = (ZipStorer.Compression)method; item.FilenameInZip = encoding.GetString(this.CentralDirImage, i + 46, (int)num2); item.FileOffset = this.GetFileOffset(headerOffset); item.FileSize = fileSize; item.CompressedSize = compressedSize; item.HeaderOffset = headerOffset; item.HeaderSize = headerSize; item.Crc32 = crc; item.ModifyTime = this.DosTimeToDateTime(dt); if (num4 > 0) { item.Comment = encoding.GetString(this.CentralDirImage, i + 46 + (int)num2 + (int)num3, (int)num4); } list.Add(item); } return(list); }
public bool ExtractFile(ZipStorer.ZipFileEntry _zfe, Stream _stream) { if (!_stream.CanWrite) { throw new InvalidOperationException("Stream cannot be written"); } byte[] array = new byte[4]; this.ZipFileStream.Seek((long)((ulong)_zfe.HeaderOffset), SeekOrigin.Begin); this.ZipFileStream.Read(array, 0, 4); if (BitConverter.ToUInt32(array, 0) != 67324752u) { return(false); } Stream stream; if (_zfe.Method == ZipStorer.Compression.Store) { stream = this.ZipFileStream; } else { if (_zfe.Method != ZipStorer.Compression.Deflate) { return(false); } stream = new DeflateStream(this.ZipFileStream, CompressionMode.Decompress, true); } byte[] array2 = new byte[16384]; this.ZipFileStream.Seek((long)((ulong)_zfe.FileOffset), SeekOrigin.Begin); int num2; for (uint num = _zfe.FileSize; num > 0u; num -= (uint)num2) { num2 = stream.Read(array2, 0, (int)Math.Min((long)((ulong)num), (long)array2.Length)); _stream.Write(array2, 0, num2); } _stream.Flush(); if (_zfe.Method == ZipStorer.Compression.Deflate) { stream.Dispose(); } return(true); }
public bool ExtractFile(ZipStorer.ZipFileEntry _zfe, string _filename) { string directoryName = Path.GetDirectoryName(_filename); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } if (Directory.Exists(_filename)) { return(true); } Stream stream = new FileStream(_filename, FileMode.Create, FileAccess.Write); bool flag = this.ExtractFile(_zfe, stream); if (flag) { stream.Close(); } File.SetCreationTime(_filename, _zfe.ModifyTime); File.SetLastWriteTime(_filename, _zfe.ModifyTime); return(flag); }