Save() method in Ionic Zip, this method allows the application to explicitly specify the name of the zip file when saving. Use this when creating a new zip file, or when updating a zip archive.
The ZipFile instance is written to storage, typically a zip file in a filesystem, only when the caller calls Save. The Save operation writes the zip content to a temporary file, and then renames the temporary file to the desired name. If necessary, this method will delete a pre-existing file before the rename.
using (ZipFile zip = ZipFile.Read("Archive.zip")){zip.AddFile("test.jpg");zip.Save("UpdatedArchive.zip");}
There is an Save Progress event handler which invoked when a Save() starts, before and after each entry has been written to the archive, when a Save() completes, and during other Save events.
Depending on the particular event, different properties on the SaveProgressEventArgs parameter are set.
The following EventTypes describes under which this event handler is invoked with the given EventType.
ZipProgressEventType.Saving_Started Fired when ZipFile.Save() begins.
ZipProgressEventType.Saving_BeforeSaveEntry Fired within ZipFile.Save(), just before writing data for each particular entry.
ZipProgressEventType.Saving_AfterSaveEntry Fired within ZipFile.Save(), just after having finished writing data for each particular entry.
ZipProgressEventType.Saving_Completed Fired when ZipFile.Save() has completed.
ZipProgressEventType.Saving_AfterSaveTempArchive Fired after the temporary file has been created. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_BeforeRenameTempArchive Fired just before renaming the temporary file to the permanent location. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_AfterRenameTempArchive Fired just after renaming the temporary file to the permanent location. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_AfterCompileSelfExtractor Fired after a self-extracting archive has finished compiling. This EventType is used only within SaveSelfExtractor().
ZipProgressEventType.Saving_BytesRead Set during the save of a particular entry, to update progress of the Save(). When this EventType is set, the BytesTransferred is the number of bytes that have been read from the source stream. The TotalBytesToTransfer is the number of bytes in the uncompressed file.
In example below to see how it can be used.
static bool justHadByteUpdate= false;
public static void SaveProgress(object sender, SaveProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Saving_Started)
Console.WriteLine("Saving: {0}", e.ArchiveName);
else if (e.EventType == ZipProgressEventType.Saving_Completed)
{
justHadByteUpdate= false;
Console.WriteLine();
Console.WriteLine("Done: {0}", e.ArchiveName);
}
else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry)
{
if (justHadByteUpdate)
Console.WriteLine();
Console.WriteLine(" Writing: {0} ({1}/{2})",
e.CurrentEntry.FileName, e.EntriesSaved, e.EntriesTotal);
justHadByteUpdate= false;
}
else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
{
if (justHadByteUpdate)
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(" {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer,
e.BytesTransferred / (0.01 * e.TotalBytesToTransfer ));
justHadByteUpdate= true;
}
}
public static ZipUp(string targetZip, string directory)
{
using (var zip = new ZipFile()) {
zip.SaveProgress += SaveProgress;
zip.AddDirectory(directory);
zip.Save(targetZip);
}
}
I have copied this information from Ionic help file for my future reference.
No comments:
Post a Comment