public ZipEntry PutNextEntry(string entryName) { if (string.IsNullOrEmpty(entryName)) { throw new ArgumentNullException("entryName"); } if (_disposed) { _exceptionPending = true; throw new InvalidOperationException("The stream has been closed."); } _FinishCurrentEntry(); _currentEntry = ZipEntry.CreateForZipOutputStream(entryName); _currentEntry._container = new ZipContainer(this); _currentEntry._BitField |= 8; _currentEntry.SetEntryTimes(DateTime.Now, DateTime.Now, DateTime.Now); _currentEntry.CompressionLevel = CompressionLevel; _currentEntry.CompressionMethod = CompressionMethod; _currentEntry.Password = _password; _currentEntry.Encryption = Encryption; _currentEntry.AlternateEncoding = AlternateEncoding; _currentEntry.AlternateEncodingUsage = AlternateEncodingUsage; if (entryName.EndsWith("/")) { _currentEntry.MarkAsDirectory(); } _currentEntry.EmitTimesInWindowsFormatWhenSaving = ((_timestamp & ZipEntryTimestamp.Windows) != 0); _currentEntry.EmitTimesInUnixFormatWhenSaving = ((_timestamp & ZipEntryTimestamp.Unix) != 0); InsureUniqueEntry(_currentEntry); _needToWriteEntryHeader = true; return(_currentEntry); }
public ZipEntry AddEntry(string entryName, Stream stream) { ZipEntry ze = ZipEntry.CreateForStream(entryName, stream); ze.SetEntryTimes(DateTime.Now, DateTime.Now, DateTime.Now); if (Verbose) { StatusMessageTextWriter.WriteLine("adding {0}...", entryName); } return(_InternalAddEntry(ze)); }
/// <summary> /// Add an entry, for which the application will provide a stream /// containing the entry data, on a just-in-time basis. /// </summary> /// /// <remarks> /// <para> /// In cases where the application wishes to open the stream that /// holds the content for the ZipEntry, on a just-in-time basis, the /// application can use this method. The application provides an /// opener delegate that will be called by the DotNetZip library to /// obtain a readable stream that can be read to get the bytes for /// the given entry. Typically, this delegate opens a stream. /// Optionally, the application can provide a closer delegate as /// well, which will be called by DotNetZip when all bytes have been /// read from the entry. /// </para> /// /// <para> /// These delegates are called from within the scope of the call to /// ZipFile.Save(). /// </para> /// /// <para> /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see /// cref="Password"/>, <see cref="SetCompression"/>, <see /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>, /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their /// respective values at the time of this call will be applied to the /// <c>ZipEntry</c> added. /// </para> /// /// </remarks> /// /// <example> /// /// This example uses anonymous methods in C# to open and close the /// source stream for the content for a zip entry. /// /// <code lang="C#"> /// using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) /// { /// zip.AddEntry(zipEntryName, /// (name) => File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ), /// (name, stream) => stream.Close() /// ); /// /// zip.Save(zipFileName); /// } /// </code> /// /// </example> /// /// <example> /// /// This example uses delegates in VB.NET to open and close the /// the source stream for the content for a zip entry. VB 9.0 lacks /// support for "Sub" lambda expressions, and so the CloseDelegate must /// be an actual, named Sub. /// /// <code lang="VB"> /// /// Function MyStreamOpener(ByVal entryName As String) As Stream /// '' This simply opens a file. You probably want to do somethinig /// '' more involved here: open a stream to read from a database, /// '' open a stream on an HTTP connection, and so on. /// Return File.OpenRead(entryName) /// End Function /// /// Sub MyStreamCloser(entryName As String, stream As Stream) /// stream.Close() /// End Sub /// /// Public Sub Run() /// Dim dirToZip As String = "fodder" /// Dim zipFileToCreate As String = "Archive.zip" /// Dim opener As OpenDelegate = AddressOf MyStreamOpener /// Dim closer As CloseDelegate = AddressOf MyStreamCloser /// Dim numFilestoAdd As Int32 = 4 /// Using zip As ZipFile = New ZipFile /// Dim i As Integer /// For i = 0 To numFilesToAdd - 1 /// zip.AddEntry(String.Format("content-{0:000}.txt"), opener, closer) /// Next i /// zip.Save(zipFileToCreate) /// End Using /// End Sub /// /// </code> /// </example> /// /// <param name="entryName">the name of the entry to add</param> /// <param name="opener"> /// the delegate that will be invoked by ZipFile.Save() to get the /// readable stream for the given entry. ZipFile.Save() will call /// read on this stream to obtain the data for the entry. This data /// will then be compressed and written to the newly created zip /// file. /// </param> /// <param name="closer"> /// the delegate that will be invoked to close the stream. This may /// be null (Nothing in VB), in which case no call is makde to close /// the stream. /// </param> /// <returns>the ZipEntry added</returns> /// public ZipEntry AddEntry(string entryName, OpenDelegate opener, CloseDelegate closer) { ZipEntry ze = ZipEntry.CreateForJitStreamProvider(entryName, opener, closer); ze.SetEntryTimes(DateTime.Now, DateTime.Now, DateTime.Now); if (Verbose) { StatusMessageTextWriter.WriteLine("adding {0}...", entryName); } return(_InternalAddEntry(ze)); }
// private string DictionaryKeyForEntry(ZipEntry ze1) // { // var filename = SharedUtilities.NormalizePathForUseInZipFile(ze1.FileName); // return filename; // } /// <summary> /// Creates a directory in the zip archive. /// </summary> /// /// <remarks> /// /// <para> /// Use this when you want to create a directory in the archive but there is /// no corresponding filesystem representation for that directory. /// </para> /// /// <para> /// You will probably not need to do this in your code. One of the only times /// you will want to do this is if you want an empty directory in the zip /// archive. The reason: if you add a file to a zip archive that is stored /// within a multi-level directory, all of the directory tree is implicitly /// created in the zip archive. /// </para> /// /// </remarks> /// /// <param name="directoryNameInArchive"> /// The name of the directory to create in the archive. /// </param> /// <returns>The <c>ZipEntry</c> added.</returns> public ZipEntry AddDirectoryByName(string directoryNameInArchive) { // workitem 9073 ZipEntry dir = ZipEntry.CreateFromNothing(directoryNameInArchive); dir._container = new ZipContainer(this); dir.MarkAsDirectory(); dir.AlternateEncoding = this.AlternateEncoding; // workitem 8984 dir.AlternateEncodingUsage = this.AlternateEncodingUsage; dir.SetEntryTimes(DateTime.Now, DateTime.Now, DateTime.Now); dir.EmitTimesInWindowsFormatWhenSaving = _emitNtfsTimes; dir.EmitTimesInUnixFormatWhenSaving = _emitUnixTimes; dir._Source = ZipEntrySource.Stream; //string key = DictionaryKeyForEntry(dir); InternalAddEntry(dir.FileName, dir); AfterAddEntry(dir); return(dir); }
/// <summary> /// Specify the name of the next entry that will be written to the zip file. /// </summary> /// /// <remarks> /// <para> /// Call this method just before calling <see cref="Write(byte[], int, int)"/>, to /// specify the name of the entry that the next set of bytes written to /// the <c>ZipOutputStream</c> belongs to. All subsequent calls to <c>Write</c>, /// until the next call to <c>PutNextEntry</c>, /// will be inserted into the named entry in the zip file. /// </para> /// /// <para> /// If the <paramref name="entryName"/> used in <c>PutNextEntry()</c> ends in /// a slash, then the entry added is marked as a directory. Because directory /// entries do not contain data, a call to <c>Write()</c>, before an /// intervening additional call to <c>PutNextEntry()</c>, will throw an /// exception. /// </para> /// /// <para> /// If you don't call <c>Write()</c> between two calls to /// <c>PutNextEntry()</c>, the first entry is inserted into the zip file as a /// file of zero size. This may be what you want. /// </para> /// /// <para> /// Because <c>PutNextEntry()</c> closes out the prior entry, if any, this /// method may throw if there is a problem with the prior entry. /// </para> /// /// <para> /// This method returns the <c>ZipEntry</c>. You can modify public properties /// on the <c>ZipEntry</c>, such as <see cref="ZipEntry.Encryption"/>, <see /// cref="ZipEntry.Password"/>, and so on, until the first call to /// <c>ZipOutputStream.Write()</c>, or until the next call to /// <c>PutNextEntry()</c>. If you modify the <c>ZipEntry</c> <em>after</em> /// having called <c>Write()</c>, you may get a runtime exception, or you may /// silently get an invalid zip archive. /// </para> /// /// </remarks> /// /// <example> /// /// This example shows how to create a zip file, using the /// <c>ZipOutputStream</c> class. /// /// <code> /// private void Zipup() /// { /// using (FileStream fs raw = File.Open(_outputFileName, FileMode.Create, FileAccess.ReadWrite )) /// { /// using (var output= new ZipOutputStream(fs)) /// { /// output.Password = "******"; /// output.Encryption = EncryptionAlgorithm.WinZipAes256; /// output.PutNextEntry("entry1.txt"); /// byte[] buffer= System.Text.Encoding.ASCII.GetBytes("This is the content for entry #1."); /// output.Write(buffer,0,buffer.Length); /// output.PutNextEntry("entry2.txt"); // this will be zero length /// output.PutNextEntry("entry3.txt"); /// buffer= System.Text.Encoding.ASCII.GetBytes("This is the content for entry #3."); /// output.Write(buffer,0,buffer.Length); /// } /// } /// } /// </code> /// </example> /// /// <param name="entryName"> /// The name of the entry to be added, including any path to be used /// within the zip file. /// </param> /// /// <returns> /// The ZipEntry created. /// </returns> /// public ZipEntry PutNextEntry(String entryName) { if (_disposed) { _exceptionPending = true; throw new System.InvalidOperationException("The stream has been closed."); } _FinishCurrentEntry(); _currentEntry = ZipEntry.CreateForZipOutputStream(entryName); _currentEntry._container = new ZipContainer(this); _currentEntry._BitField |= 0x0008; // workitem 8932 _currentEntry.SetEntryTimes(DateTime.Now, DateTime.Now, DateTime.Now); _currentEntry.CompressionLevel = CompressionLevel; _currentEntry.Encryption = Encryption; _currentEntry.Password = _password; if (entryName.EndsWith("/")) _currentEntry.MarkAsDirectory(); _currentEntry.EmitTimesInWindowsFormatWhenSaving = ((_timestamp & ZipEntryTimestamp.Windows) != 0); _currentEntry.EmitTimesInUnixFormatWhenSaving = ((_timestamp & ZipEntryTimestamp.Unix) != 0); InsureUniqueEntry(_currentEntry); _needToWriteEntryHeader = true; return _currentEntry; }