/// <summary> /// Archives a file by compressing it and adding it to the zip archive. /// </summary> /// <param name="sourceFileName">The path to the file to be archived. You can specify either a relative or an absolute path. A relative path is interpreted as relative to the current working directory.</param> /// <param name="entryName">The name of the entry to create in the zip archive.</param> /// <returns>A wrapper for the new entry in the zip archive.</returns> /// <exception cref="T:System.ArgumentException"><paramref name="sourceFileName" /> is <see cref="F:System.String.Empty" />, contains only white space, or contains at least one invalid character.-or-<paramref name="entryName" /> is <see cref="F:System.String.Empty" />.</exception> /// <exception cref="T:System.ArgumentNullException"><paramref name="sourceFileName" /> or <paramref name="entryName" /> is null.</exception> /// <exception cref="T:System.IO.PathTooLongException">In <paramref name="sourceFileName" />, the specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must not exceed 248 characters, and file names must not exceed 260 characters.</exception> /// <exception cref="T:System.IO.DirectoryNotFoundException"><paramref name="sourceFileName" /> is invalid (for example, it is on an unmapped drive).</exception> /// <exception cref="T:System.IO.IOException">The file specified by <paramref name="sourceFileName" /> cannot be opened.</exception> /// <exception cref="T:System.UnauthorizedAccessException"><paramref name="sourceFileName" /> specifies a directory.-or-The caller does not have the required permission to access the file specified by <paramref name="sourceFileName" />.</exception> /// <exception cref="T:System.IO.FileNotFoundException">The file specified by <paramref name="sourceFileName" /> is not found.</exception> /// <exception cref="T:System.NotSupportedException">The <paramref name="sourceFileName" /> parameter is in an invalid format.-or-The zip archive does not support writing.</exception> /// <exception cref="T:System.ObjectDisposedException">The zip archive has been disposed.</exception> public ZipArchiveEntry CreateEntryFromFile(string sourceFileName, string entryName) { if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName"); } if (entryName == null) { throw new ArgumentNullException("entryName"); } using (Stream stream = File.Open(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { ZipArchiveEntry zipArchiveEntry = this.CreateEntry(entryName, File.GetAttributes(sourceFileName)); DateTime dateTime = File.GetLastWriteTime(sourceFileName); if (dateTime.Year < 1980 || dateTime.Year > 2107) { dateTime = new DateTime(1980, 1, 1, 0, 0, 0); } zipArchiveEntry.LastWriteTime = (DateTimeOffset)dateTime; using (Stream destination = zipArchiveEntry.Open()) { ZipHelper.CopyStreamTo(stream, destination); } return(zipArchiveEntry); } }
private void WriteLocalFileHeaderAndDataIfNeeded() { if (this._storedUncompressedData != null || this._compressedBytes != null) { if (this._storedUncompressedData != null) { this._uncompressedSize = this._storedUncompressedData.Length; using (Stream destination = new ZipArchiveEntry.DirectToArchiveWriterStream(this.GetDataCompressor(this._archive.ArchiveStream, true, (EventHandler)null), this)) { this._storedUncompressedData.Seek(0L, SeekOrigin.Begin); ZipHelper.CopyStreamTo(this._storedUncompressedData, destination); this._storedUncompressedData.Close(); this._storedUncompressedData = null; } } else { if (this._uncompressedSize == 0L) { this.CompressionMethod = ZipArchiveEntry.CompressionMethodValues.Stored; } this.WriteLocalFileHeader(false); using (MemoryStream memoryStream = new MemoryStream(this._compressedBytes)) { ZipHelper.CopyStreamTo(memoryStream, this._archive.ArchiveStream); } } } else { if (this._archive.Mode != ZipArchiveMode.Update && this._everOpenedForWrite) { return; } this._everOpenedForWrite = true; this.WriteLocalFileHeader(true); } }
/// <summary> /// Archives a stream by compressing it and adding it to the zip archive. /// </summary> /// <param name="sourceStream">The source stream to compress.</param> /// <param name="entryName">The name of the entry to create in the zip archive.</param> /// <param name="entryAttributes">The entry attributes.</param> /// <returns>A wrapper for the new entry in the zip archive.</returns> public ZipArchiveEntry CreateEntryFromStream(Stream sourceStream, string entryName, FileAttributes entryAttributes = FileAttributes.Normal) { if (sourceStream == null) { throw new ArgumentNullException("sourceStream"); } if (entryName == null) { throw new ArgumentNullException("entryName"); } ZipArchiveEntry zipArchiveEntry = this.CreateEntry(entryName, entryAttributes); zipArchiveEntry.LastWriteTime = DateTimeOffset.Now; using (Stream destination = zipArchiveEntry.Open()) { ZipHelper.CopyStreamTo(sourceStream, destination); } return(zipArchiveEntry); }
/// <summary> /// Extracts an entry in the zip archive to a directory, and optionally overwrites an existing directory attributes that has the same name. /// </summary> /// <param name="destinationFileName">The path of the file to create from the contents of the entry. You can specify either a relative or an absolute path. A relative path is interpreted as relative to the current working directory.</param> /// <param name="overwrite">true to overwrite an existing directory attributes that has the same name as the destination directory; otherwise, false.</param> public void ExtractToFile(string destinationFileName, bool overwrite) { if (destinationFileName == null) { throw new ArgumentNullException("destinationFileName"); } Directory.CreateDirectory(Path.GetDirectoryName(destinationFileName)); FileMode mode = overwrite ? FileMode.Create : FileMode.CreateNew; using (Stream destination = File.Open(destinationFileName, mode, FileAccess.Write, FileShare.None)) { using (Stream stream = this.Open()) { ZipHelper.CopyStreamTo(stream, destination); } } File.SetLastWriteTime(destinationFileName, this.LastWriteTime.DateTime); File.SetAttributes(destinationFileName, this.Attributes); }
private void Init(Stream stream, ZipArchiveMode mode, bool leaveOpen) { Stream stream1 = null; try { this._backingStream = null; switch (mode) { case ZipArchiveMode.Read: if (!stream.CanRead) { throw new ArgumentException(CompressionConstants.ReadModeCapabilities); } if (!stream.CanSeek) { this._backingStream = stream; stream1 = stream = new MemoryStream(); ZipHelper.CopyStreamTo(this._backingStream, stream); stream.Seek(0L, SeekOrigin.Begin); break; } break; case ZipArchiveMode.Create: if (!stream.CanWrite) { throw new ArgumentException(CompressionConstants.CreateModeCapabilities); } break; case ZipArchiveMode.Update: if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek) { throw new ArgumentException(CompressionConstants.UpdateModeCapabilities); } break; default: throw new ArgumentOutOfRangeException("mode"); } this._mode = mode; this._archiveStream = stream; this._archiveStreamOwner = null; this._archiveReader = mode != ZipArchiveMode.Create ? new BinaryReader(stream) : null; this._entries = new List <ZipArchiveEntry>(); this._entriesCollection = new ReadOnlyCollection <ZipArchiveEntry>(this._entries); this._entriesDictionary = new Dictionary <string, ZipArchiveEntry>(); this._readEntries = false; this._leaveOpen = leaveOpen; this._centralDirectoryStart = 0L; this._isDisposed = false; this._numberOfThisDisk = 0U; this._archiveComment = null; switch (mode) { case ZipArchiveMode.Read: this.ReadEndOfCentralDirectory(); break; case ZipArchiveMode.Create: this._readEntries = true; break; default: if (this._archiveStream.Length == 0L) { this._readEntries = true; break; } this.ReadEndOfCentralDirectory(); this.EnsureCentralDirectoryRead(); using (List <ZipArchiveEntry> .Enumerator enumerator = this._entries.GetEnumerator()) { while (enumerator.MoveNext()) { enumerator.Current.ThrowIfNotOpenable(false, true); } break; } } } catch { if (stream1 != null) { stream1.Close(); } throw; } }