Exemplo n.º 1
0
 public ZipArchiveFile this[string archivePath]
 {
     get
     {
         ZipArchiveFile zipArchiveFile = null;
         this.entries.TryGetValue(archivePath, out zipArchiveFile);
         return(zipArchiveFile);
     }
 }
Exemplo n.º 2
0
        public TextWriter CreateText(string archivePath)
        {
            ZipArchiveFile zipArchiveFile;

            if (!this.entries.TryGetValue(archivePath, out zipArchiveFile))
            {
                zipArchiveFile = new ZipArchiveFile(this, archivePath);
            }
            return(zipArchiveFile.CreateText());
        }
Exemplo n.º 3
0
        public void WriteAllText(string archivePath, string data)
        {
            ZipArchiveFile zipArchiveFile;

            if (!this.entries.TryGetValue(archivePath, out zipArchiveFile))
            {
                zipArchiveFile = new ZipArchiveFile(this, archivePath);
            }
            zipArchiveFile.WriteAllText(data);
        }
Exemplo n.º 4
0
 public void CopyFromFile(string sourceFilePath, string targetArchivePath)
 {
     using (Stream fileStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Write | FileShare.ReadWrite | FileShare.Delete))
     {
         using (Stream stream = this.Create(targetArchivePath))
         {
             ZipArchiveFile.CopyStream(fileStream, stream);
         }
     }
     this[targetArchivePath].LastWriteTime = File.GetLastWriteTime(sourceFilePath);
 }
Exemplo n.º 5
0
 public void CopyTo(string outputArchivePath)
 {
     using (Stream stream = this.archive.Create(outputArchivePath))
     {
         using (Stream stream1 = this.OpenRead())
         {
             ZipArchiveFile.CopyStream(stream1, stream);
         }
     }
     this.archive[outputArchivePath].LastWriteTime = this.LastWriteTime;
 }
Exemplo n.º 6
0
        public void CopyToFile(string outputFilePath)
        {
            string directoryName = Path.GetDirectoryName(outputFilePath);

            if (directoryName.Length > 0)
            {
                Directory.CreateDirectory(directoryName);
            }
            using (Stream fileStream = new FileStream(outputFilePath, FileMode.Create))
            {
                using (Stream stream = this.OpenRead())
                {
                    ZipArchiveFile.CopyStream(stream, fileStream);
                }
            }
            File.SetLastWriteTime(outputFilePath, this.LastWriteTime);
        }
Exemplo n.º 7
0
        private void WriteZipFileHeader(Stream writer)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(this.name.Replace(Path.DirectorySeparatorChar, '/'));
            if ((ulong)((uint)this.length) != this.length)
            {
                throw new ApplicationException("File length too long.");
            }
            this.headerOffset = (uint)writer.Position;
            ByteBuffer byteBuffer = new ByteBuffer(30);

            byteBuffer.WriteUInt32(67324752);
            byteBuffer.WriteUInt16(256);
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt16((ushort)this.compressionMethod);
            byteBuffer.WriteUInt32(ZipArchiveFile.DateTimeToDosTime(this.lastWriteTime));
            byteBuffer.WriteUInt32(this.CheckSum);
            byteBuffer.WriteUInt32((uint)this.compressedLength);
            byteBuffer.WriteUInt32((uint)this.Length);
            byteBuffer.WriteUInt16((ushort)((int)bytes.Length));
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteContentsTo(writer);
            writer.Write(bytes, 0, (int)bytes.Length);
        }
Exemplo n.º 8
0
        internal void WriteArchiveDirectoryEntryToStream(Stream writer)
        {
            byte[]     bytes      = Encoding.UTF8.GetBytes(this.name);
            ByteBuffer byteBuffer = new ByteBuffer(46);

            byteBuffer.WriteUInt32(33639248);
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt16(256);
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt16((ushort)this.compressionMethod);
            byteBuffer.WriteUInt32(ZipArchiveFile.DateTimeToDosTime(this.lastWriteTime));
            byteBuffer.WriteUInt32(this.CheckSum);
            byteBuffer.WriteUInt32((uint)this.compressedLength);
            byteBuffer.WriteUInt32((uint)this.Length);
            byteBuffer.WriteUInt16((ushort)((int)bytes.Length));
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt16(0);
            byteBuffer.WriteUInt32(0);
            byteBuffer.WriteUInt32(this.headerOffset);
            byteBuffer.WriteContentsTo(writer);
            writer.Write(bytes, 0, (int)bytes.Length);
        }
Exemplo n.º 9
0
 private void Read(Stream archiveStream)
 {
     while (ZipArchiveFile.Read(this) != null)
     {
     }
 }
Exemplo n.º 10
0
        internal static ZipArchiveFile Read(ZipArchive archive)
        {
            Stream     stream     = archive.fromStream;
            ByteBuffer byteBuffer = new ByteBuffer(30);
            int        num        = byteBuffer.ReadContentsFrom(stream);

            if (num == 0)
            {
                return(null);
            }
            uint num1 = byteBuffer.ReadUInt32();

            if (num1 != 67324752)
            {
                if (num1 != 33639248)
                {
                    throw new ApplicationException("Bad ZipFile Header");
                }
                return(null);
            }
            if (byteBuffer.ReadUInt16() > 256)
            {
                throw new ApplicationException("Zip file requires unsupported features");
            }
            byteBuffer.SkipBytes(2);
            ZipArchiveFile zipArchiveFile = new ZipArchiveFile(archive, null)
            {
                compressionMethod = (ZipArchiveFile.CompressionMethod)byteBuffer.ReadUInt16(),
                lastWriteTime     = ZipArchiveFile.DosTimeToDateTime(byteBuffer.ReadUInt32()),
                crc32             = new uint?(byteBuffer.ReadUInt32()),
                compressedLength  = (void *)(checked ((int)byteBuffer.ReadUInt32())),
                length            = (long)byteBuffer.ReadUInt32()
            };
            int num2 = byteBuffer.ReadUInt16();

            byte[] numArray = new byte[num2];
            int    num3     = stream.Read(numArray, 0, num2);

            zipArchiveFile.name = Encoding.UTF8.GetString(numArray).Replace('/', Path.DirectorySeparatorChar);
            archive.entries[zipArchiveFile.name] = zipArchiveFile;
            if (num != byteBuffer.Length || num3 != num2 || num2 == 0 || zipArchiveFile.LastWriteTime.Ticks == (long)0)
            {
                throw new ApplicationException("Bad Zip File Header");
            }
            if (zipArchiveFile.Name.IndexOfAny(ZipArchiveFile.invalidPathChars) >= 0)
            {
                throw new ApplicationException("Invalid File Name");
            }
            if (zipArchiveFile.compressionMethod != ZipArchiveFile.CompressionMethod.None && zipArchiveFile.compressionMethod != ZipArchiveFile.CompressionMethod.Deflate)
            {
                throw new ApplicationException(string.Concat("Unsupported compression mode ", zipArchiveFile.compressionMethod));
            }
            if (!archive.IsReadOnly || !stream.CanSeek)
            {
                zipArchiveFile.compressedData = new byte[zipArchiveFile.compressedLength];
                stream.Read(zipArchiveFile.compressedData, 0, zipArchiveFile.compressedLength);
            }
            else
            {
                zipArchiveFile.positionOfCompressedDataInArchive = archive.fromStream.Position;
                stream.Seek((long)zipArchiveFile.compressedLength, SeekOrigin.Current);
            }
            return(zipArchiveFile);
        }