Represents an entry in Zip file directory
Beispiel #1
0
        private void WriteLocalHeader(ref ZipStorer.ZipFileEntry zipFileEntry)
        {
            long     position = this.zipFileStream.Position;
            Encoding encoding = zipFileEntry.EncodeUTF8 ? Encoding.UTF8 : ZipStorer.defaultEncoding;

            byte[] bytes = encoding.GetBytes(zipFileEntry.FilenameInZip);
            this.zipFileStream.Write(new byte[]
            {
                80,
                75,
                3,
                4,
                20,
                0
            }, 0, 6);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.EncodeUTF8 ? 2048 : 0), 0, 2);
            this.zipFileStream.Write(BitConverter.GetBytes((ushort)zipFileEntry.Method), 0, 2);
            this.zipFileStream.Write(BitConverter.GetBytes(ZipStorer.DateTimeToDosTime(zipFileEntry.ModifyTime)), 0, 4);
            Stream arg_C2_0 = this.zipFileStream;

            byte[] buffer = new byte[12];
            arg_C2_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);
            zipFileEntry.HeaderSize = (uint)(this.zipFileStream.Position - position);
        }
Beispiel #2
0
        private void WriteCentralDirRecord(ZipStorer.ZipFileEntry zipFileEntry)
        {
            Encoding encoding = zipFileEntry.EncodeUTF8 ? Encoding.UTF8 : ZipStorer.defaultEncoding;

            byte[] bytes  = encoding.GetBytes(zipFileEntry.FilenameInZip);
            byte[] bytes2 = encoding.GetBytes(zipFileEntry.Comment);
            this.zipFileStream.Write(new byte[]
            {
                80,
                75,
                1,
                2,
                23,
                11,
                20,
                0
            }, 0, 8);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.EncodeUTF8 ? 2048 : 0), 0, 2);
            this.zipFileStream.Write(BitConverter.GetBytes((ushort)zipFileEntry.Method), 0, 2);
            this.zipFileStream.Write(BitConverter.GetBytes(ZipStorer.DateTimeToDosTime(zipFileEntry.ModifyTime)), 0, 4);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.Crc32), 0, 4);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.CompressedSize), 0, 4);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.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(zipFileEntry.HeaderOffset), 0, 4);
            this.zipFileStream.Write(bytes, 0, bytes.Length);
            this.zipFileStream.Write(bytes2, 0, bytes2.Length);
        }
Beispiel #3
0
        private void UpdateCrcAndSizes(ref ZipStorer.ZipFileEntry zipFileEntry)
        {
            long position = this.zipFileStream.Position;

            this.zipFileStream.Position = (long)((ulong)(zipFileEntry.HeaderOffset + 8u));
            this.zipFileStream.Write(BitConverter.GetBytes((ushort)zipFileEntry.Method), 0, 2);
            this.zipFileStream.Position = (long)((ulong)(zipFileEntry.HeaderOffset + 14u));
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.Crc32), 0, 4);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.CompressedSize), 0, 4);
            this.zipFileStream.Write(BitConverter.GetBytes(zipFileEntry.FileSize), 0, 4);
            this.zipFileStream.Position = position;
        }
Beispiel #4
0
        private void Store(ref ZipStorer.ZipFileEntry zipFileEntry, Stream sourceStream)
        {
            byte[] array     = new byte[16384];
            uint   num       = 0u;
            long   position  = this.zipFileStream.Position;
            long   position2 = sourceStream.Position;
            Stream stream;

            if (zipFileEntry.Method == ZipStorer.CompressionMethod.Store)
            {
                stream = this.zipFileStream;
            }
            else
            {
                stream = new DeflateStream(this.zipFileStream, CompressionMode.Compress, true);
            }
            zipFileEntry.Crc32 = 4294967295u;
            int num2;

            do
            {
                num2 = sourceStream.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))
                    {
                        zipFileEntry.Crc32 = (ZipStorer.crcTable[(int)((UIntPtr)((zipFileEntry.Crc32 ^ (uint)array[(int)((UIntPtr)num3)]) & 255u))] ^ zipFileEntry.Crc32 >> 8);
                        num3 += 1u;
                    }
                }
            }while (num2 == array.Length);
            stream.Flush();
            if (zipFileEntry.Method == ZipStorer.CompressionMethod.Deflate)
            {
                stream.Dispose();
            }
            zipFileEntry.Crc32         ^= 4294967295u;
            zipFileEntry.FileSize       = num;
            zipFileEntry.CompressedSize = (uint)(this.zipFileStream.Position - position);
            if (zipFileEntry.Method == ZipStorer.CompressionMethod.Deflate && !this.ForceDeflating && sourceStream.CanSeek && zipFileEntry.CompressedSize > zipFileEntry.FileSize)
            {
                zipFileEntry.Method         = ZipStorer.CompressionMethod.Store;
                this.zipFileStream.Position = position;
                this.zipFileStream.SetLength(position);
                sourceStream.Position = position2;
                this.Store(ref zipFileEntry, sourceStream);
            }
        }
Beispiel #5
0
        public List <ZipStorer.ZipFileEntry> ReadCentralDirectory()
        {
            if (this.centralDirectoryImage == 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.centralDirectoryImage.Length; i += (int)(46 + num2 + num3 + num4))
            {
                uint num = BitConverter.ToUInt32(this.centralDirectoryImage, i);
                if (num != 33639248u)
                {
                    break;
                }
                bool   flag           = (BitConverter.ToUInt16(this.centralDirectoryImage, i + 8) & 2048) != 0;
                ushort method         = BitConverter.ToUInt16(this.centralDirectoryImage, i + 10);
                uint   dosTime        = BitConverter.ToUInt32(this.centralDirectoryImage, i + 12);
                uint   crc            = BitConverter.ToUInt32(this.centralDirectoryImage, i + 16);
                uint   compressedSize = BitConverter.ToUInt32(this.centralDirectoryImage, i + 20);
                uint   fileSize       = BitConverter.ToUInt32(this.centralDirectoryImage, i + 24);
                num2 = BitConverter.ToUInt16(this.centralDirectoryImage, i + 28);
                num3 = BitConverter.ToUInt16(this.centralDirectoryImage, i + 30);
                num4 = BitConverter.ToUInt16(this.centralDirectoryImage, i + 32);
                uint     headerOffset       = BitConverter.ToUInt32(this.centralDirectoryImage, 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.CompressionMethod)method;
                item.FilenameInZip  = encoding.GetString(this.centralDirectoryImage, 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     = ZipStorer.DosTimeToDateTime(dosTime);
                if (num4 > 0)
                {
                    item.Comment = encoding.GetString(this.centralDirectoryImage, i + 46 + (int)num2 + (int)num3, (int)num4);
                }
                list.Add(item);
            }
            return(list);
        }
Beispiel #6
0
        public bool ExtractFile(ZipStorer.ZipFileEntry zipFileEntry, Stream destinationStream)
        {
            if (!destinationStream.CanWrite)
            {
                throw new InvalidOperationException("Stream cannot be written");
            }
            byte[] array = new byte[4];
            this.zipFileStream.Seek((long)((ulong)zipFileEntry.HeaderOffset), SeekOrigin.Begin);
            this.zipFileStream.Read(array, 0, 4);
            if (BitConverter.ToUInt32(array, 0) != 67324752u)
            {
                return(false);
            }
            Stream stream;

            if (zipFileEntry.Method == ZipStorer.CompressionMethod.Store)
            {
                stream = this.zipFileStream;
            }
            else
            {
                if (zipFileEntry.Method != ZipStorer.CompressionMethod.Deflate)
                {
                    return(false);
                }
                stream = new DeflateStream(this.zipFileStream, CompressionMode.Decompress, true);
            }
            byte[] array2 = new byte[16384];
            this.zipFileStream.Seek((long)((ulong)zipFileEntry.FileOffset), SeekOrigin.Begin);
            int num2;

            for (uint num = zipFileEntry.FileSize; num > 0u; num -= (uint)num2)
            {
                num2 = stream.Read(array2, 0, (int)Math.Min((long)((ulong)num), (long)array2.Length));
                destinationStream.Write(array2, 0, num2);
            }
            destinationStream.Flush();
            if (zipFileEntry.Method == ZipStorer.CompressionMethod.Deflate)
            {
                stream.Dispose();
            }
            return(true);
        }
Beispiel #7
0
 public void AddStream(ZipStorer.CompressionMethod compressionMethod, Stream sourceStream, string fileNameInZip, DateTime modificationTimeStamp, string fileEntryComment)
 {
     if (this.access == FileAccess.Read)
     {
         throw new InvalidOperationException("Writing is not allowed");
     }
     ZipStorer.ZipFileEntry item = default(ZipStorer.ZipFileEntry);
     item.Method        = compressionMethod;
     item.EncodeUTF8    = this.EncodeUtf8;
     item.FilenameInZip = ZipStorer.NormalizeFileName(fileNameInZip);
     item.Comment       = ((fileEntryComment == null) ? string.Empty : fileEntryComment);
     item.Crc32         = 0u;
     item.HeaderOffset  = (uint)this.zipFileStream.Position;
     item.ModifyTime    = modificationTimeStamp;
     this.WriteLocalHeader(ref item);
     item.FileOffset = (uint)this.zipFileStream.Position;
     this.Store(ref item, sourceStream);
     sourceStream.Close();
     this.UpdateCrcAndSizes(ref item);
     this.files.Add(item);
 }
Beispiel #8
0
        public bool ExtractFile(ZipStorer.ZipFileEntry zipFileEntry, string destinationFileName)
        {
            string directoryName = Path.GetDirectoryName(destinationFileName);

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            if (Directory.Exists(destinationFileName))
            {
                return(true);
            }
            bool result = false;

            using (Stream stream = new FileStream(destinationFileName, FileMode.Create, FileAccess.Write))
            {
                result = this.ExtractFile(zipFileEntry, stream);
            }
            File.SetCreationTime(destinationFileName, zipFileEntry.ModifyTime);
            File.SetLastWriteTime(destinationFileName, zipFileEntry.ModifyTime);
            return(result);
        }
Beispiel #9
0
        public  gSheet Sheet = null;                                                                                            // This links to a data sheet if his stream implements a data sheet

        public gStream(OoXml doc, ZipStorer zip, ZipStorer.ZipFileEntry z)                                                      // This constructor is called when creating the stream from the source template
        {
            Document = doc;                                                                                                     // Save a reference to the document  
            zfe = z;                                                                                                            // Store the ZipFileEntry  
        }
Beispiel #10
0
 public PakkedFile(string name, string handle, int index, ZipStorer.ZipFileEntry entry)
 {
     Name = name;
     Handle = handle;
     IsPakked = true;
     PakIndex = index;
     Entry = entry;
 }