Skip to content

Commit

Permalink
Added FileProgressEventArgs, updated usage example.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Nov 12, 2013
1 parent 7d08f4b commit ca6b7ee
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 50 deletions.
50 changes: 46 additions & 4 deletions Unzip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public class Entry
public int DataOffset { get; set; }
}

/// <summary>
/// CRC32 calculation helper.
/// </summary>
public class Crc32Calculator
{
private static readonly uint[] Crc32Table =
Expand Down Expand Up @@ -125,12 +128,50 @@ public void UpdateWithBlock(byte[] buffer, int numberOfBytes)
}
}

/// <summary>
/// Provides data for the ExtractProgress event.
/// </summary>
public class FileProgressEventArgs : ProgressChangedEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="FileProgressEventArgs"/> class.
/// </summary>
/// <param name="currentFile">The current file.</param>
/// <param name="totalFiles">The total files.</param>
/// <param name="fileName">Name of the file.</param>
public FileProgressEventArgs(int currentFile, int totalFiles, string fileName)
: base(totalFiles != 0 ? currentFile * 100 / totalFiles : 100, fileName)
{
CurrentFile = currentFile;
TotalFiles = totalFiles;
FileName = fileName;
}

/// <summary>
/// Gets the current file.
/// </summary>
public int CurrentFile { get; private set; }

/// <summary>
/// Gets the total files.
/// </summary>
public int TotalFiles { get; private set; }

/// <summary>
/// Gets the name of the file.
/// </summary>
public string FileName { get; private set; }
}

private const int EntrySignature = 0x02014B50;
private const int FileSignature = 0x04034b50;
private const int DirectorySignature = 0x06054B50;
private const int BufferSize = 16 * 1024;

public event Action<int, int> ExtractProgress;
/// <summary>
/// Occurs when a file or a directory is extracted from an archive.
/// </summary>
public event EventHandler<FileProgressEventArgs> ExtractProgress;

/// <summary>
/// Initializes a new instance of the <see cref="Unzip" /> class.
Expand Down Expand Up @@ -194,10 +235,11 @@ public void ExtractToDirectory(string directoryName)
{
Extract(entry.Name, fileName);
}

if (ExtractProgress != null)

var extractProgress = ExtractProgress;
if (extractProgress != null)
{
ExtractProgress(index + 1, Entries.Length);
extractProgress(this, new FileProgressEventArgs(index + 1, Entries.Length, entry.Name));
}
}
}
Expand Down
96 changes: 50 additions & 46 deletions UsageExample.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
// Unzip class usage example
// Written by Alexey Yakovlev <[email protected]>
// https://github.com/yallie/unzip

using System;
using System.Linq;

namespace Internals
{
internal struct Program
{
private static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Syntax: unzip Archive.zip TargetDirectory");
return;
}

var archiveName = args.First();
var outputDirectory = args.Last();

using (var unzip = new Unzip(archiveName))
{
ListFiles(unzip);

unzip.ExtractToDirectory(outputDirectory);
}
}

private static void ListFiles(Unzip unzip)
{
var tab = unzip.Entries.Any(e => e.IsDirectory) ? "\t" : string.Empty;

foreach (var entry in unzip.Entries.OrderBy(e => e.Name))
{
if (entry.IsFile)
{
Console.WriteLine(tab + "{0}: {1} -> {2}", entry.Name, entry.CompressedSize, entry.OriginalSize);
}
else if (entry.IsDirectory)
// Unzip class usage example
// Written by Alexey Yakovlev <[email protected]>
// https://github.com/yallie/unzip

using System;
using System.Linq;

namespace Internals
{
internal struct Program
{
private static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Syntax: unzip Archive.zip TargetDirectory");
return;
}

var archiveName = args.First();
var outputDirectory = args.Last();

using (var unzip = new Unzip(archiveName))
{
Console.WriteLine("Listing files in the archive:");
ListFiles(unzip);

Console.WriteLine("Extracting files from the archive:");
unzip.ExtractProgress += (s, e) => Console.WriteLine("{0} of {1}: {2}", e.CurrentFile, e.TotalFiles, e.FileName);
unzip.ExtractToDirectory(outputDirectory);
}
}

private static void ListFiles(Unzip unzip)
{
var tab = unzip.Entries.Any(e => e.IsDirectory) ? "\t" : string.Empty;

foreach (var entry in unzip.Entries.OrderBy(e => e.Name))
{
if (entry.IsFile)
{
Console.WriteLine(entry.Name);
Console.WriteLine(tab + "{0}: {1} -> {2}", entry.Name, entry.CompressedSize, entry.OriginalSize);
continue;
}
}
}
}
}

Console.WriteLine(entry.Name);
}

Console.WriteLine();
}
}
}

0 comments on commit ca6b7ee

Please sign in to comment.