/// <summary> /// Copyies 'sourceFilePath from the textStream system to the archive as 'targetArchivePath' /// It will overwrite any existing textStream. /// </summary> public void CopyFromFile(string sourceFilePath, string targetArchivePath) { using (Stream inFile = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) using (Stream outFile = Create(targetArchivePath)) ZipArchiveFile.CopyStream(inFile, outFile); this[targetArchivePath].LastWriteTime = File.GetLastWriteTime(sourceFilePath); }
/// <summary> /// Fetch a archiveFile by name. 'archivePath' is the full path name of the archiveFile in the archive. /// It returns null if the name does not exist (and e /// </summary> public ZipArchiveFile this[string archivePath] { get { ZipArchiveFile ret = null; entries.TryGetValue(archivePath, out ret); return(ret); } }
/// <summary> /// Overwrites the archive textStream 'archivePath' with the text in 'data' /// </summary> public void WriteAllText(string archivePath, string data) { ZipArchiveFile newEntry; if (!entries.TryGetValue(archivePath, out newEntry)) { newEntry = new ZipArchiveFile(this, archivePath); } newEntry.WriteAllText(data); }
/// <summary> /// Opens a textStream in the archive for writing as a text textStream. Returns the resulting TextWriter. /// </summary> public TextWriter CreateText(string archivePath) { ZipArchiveFile newEntry; if (!entries.TryGetValue(archivePath, out newEntry)) { newEntry = new ZipArchiveFile(this, archivePath); } return(newEntry.CreateText()); }
private void Read(Stream archiveStream) { for (; ;) { ZipArchiveFile entry = ZipArchiveFile.Read(this); if (entry == null) { break; } } }
/// <summary> /// Reads a single archiveFile from a Zip Archive. Should only be used by ZipArchive. /// </summary> /// <returns>A ZipArchiveFile representing the archiveFile read from the archive.</returns> internal static ZipArchiveFile Read(ZipArchive archive) { Stream reader = archive.fromStream; ByteBuffer header = new ByteBuffer(30); int count = header.ReadContentsFrom(reader); if (count == 0) { return(null); } uint fileSignature = header.ReadUInt32(); if (fileSignature != 0x04034b50) { if (fileSignature != 0x02014b50) { throw new ApplicationException("Bad ZipFile Header"); } return(null); } ushort versionNeededToExtract = header.ReadUInt16(); if (versionNeededToExtract > 0x0100) { throw new ApplicationException("Zip file requires unsupported features"); } header.SkipBytes(2); ZipArchiveFile newEntry = new ZipArchiveFile(archive, null); newEntry.compressionMethod = (CompressionMethod)header.ReadUInt16(); newEntry.lastWriteTime = DosTimeToDateTime(header.ReadUInt32()); newEntry.crc32 = header.ReadUInt32(); newEntry.compressedLength = checked ((int)header.ReadUInt32()); newEntry.length = header.ReadUInt32(); int fileNameLength = checked ((int)header.ReadUInt16()); byte[] fileNameBuffer = new byte[fileNameLength]; int fileNameCount = reader.Read(fileNameBuffer, 0, fileNameLength); newEntry.name = Encoding.UTF8.GetString(fileNameBuffer).Replace('/', Path.DirectorySeparatorChar); archive.entries[newEntry.name] = newEntry; if (count != header.Length || fileNameCount != fileNameLength || fileNameLength == 0 || newEntry.LastWriteTime.Ticks == 0) { throw new ApplicationException("Bad Zip File Header"); } if (newEntry.Name.IndexOfAny(invalidPathChars) >= 0) { throw new ApplicationException("Invalid File Name"); } if (newEntry.compressionMethod != CompressionMethod.None && newEntry.compressionMethod != CompressionMethod.Deflate) { throw new ApplicationException("Unsupported compression mode " + newEntry.compressionMethod); } if (archive.IsReadOnly && reader.CanSeek) { newEntry.positionOfCompressedDataInArchive = archive.fromStream.Position; reader.Seek(newEntry.compressedLength, SeekOrigin.Current); } else { newEntry.compressedData = new byte[newEntry.compressedLength]; reader.Read(newEntry.compressedData, 0, (int)newEntry.compressedLength); } #if DEBUG newEntry.Validate(); #endif return(newEntry); }
/// <summary> /// Copyies 'sourceFilePath from the textStream system to the archive as 'targetArchivePath' /// It will overwrite any existing textStream. /// </summary> /// <param name="stream"></param> /// <param name="lastWriteTime"></param> /// <param name="targetArchivePath"></param> public void CopyFromStream(System.IO.Stream stream, DateTime lastWriteTime, string targetArchivePath) { using (Stream outFile = Create(targetArchivePath)) ZipArchiveFile.CopyStream(stream, outFile); this[targetArchivePath].LastWriteTime = lastWriteTime; }