Skip to content

Commit

Permalink
Added XML comments for the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Dec 2, 2012
1 parent 714ebd3 commit 6c80b71
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
76 changes: 68 additions & 8 deletions Unzip.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Unzip class for .NET 3.5
// Unzip class for .NET 3.5 Client Profile or Mono 2.10
// Written by Alexey Yakovlev <[email protected]>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand All @@ -13,7 +14,7 @@ namespace Internals
/// <summary>
/// Unzip helper class.
/// </summary>
public class Unzip : IDisposable
internal class Unzip : IDisposable
{
private const int EntrySignature = 0x02014B50;

Expand All @@ -28,32 +29,66 @@ public class Unzip : IDisposable
/// </summary>
public class Entry
{
/// <summary>
/// Gets or sets the name of a file or a directory.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets the comment.
/// </summary>
public string Comment { get; set; }

/// <summary>
/// Gets or sets the CRC32.
/// </summary>
public int Crc32 { get; set; }

/// <summary>
/// Gets or sets the compressed size of the file.
/// </summary>
public int CompressedSize { get; set; }

/// <summary>
/// Gets or sets the original size of the file.
/// </summary>
public int OriginalSize { get; set; }

public int HeaderOffset { get; set; }

public int DataOffset { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this <see cref="Entry" /> is deflated.
/// </summary>
public bool Deflated { get; set; }

/// <summary>
/// Gets a value indicating whether this <see cref="Entry" /> is a directory.
/// </summary>
public bool IsDirectory { get { return Name.EndsWith("/"); } }

/// <summary>
/// Gets a value indicating whether this <see cref="Entry" /> is a file.
/// </summary>
public bool IsFile { get { return !IsDirectory; } }

[EditorBrowsable(EditorBrowsableState.Never)]
public int HeaderOffset { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public int DataOffset { get; set; }
}

/// <summary>
/// Initializes a new instance of the <see cref="Unzip" /> class.
/// </summary>
/// <param name="fileName">Name of the file.</param>
public Unzip(string fileName)
: this(File.OpenRead(fileName))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Unzip" /> class.
/// </summary>
/// <param name="stream">The stream.</param>
public Unzip(Stream stream)
{
Stream = stream;
Expand All @@ -64,6 +99,10 @@ public Unzip(Stream stream)

private BinaryReader Reader { get; set; }

/// <summary>
/// Performs application-defined tasks associated with
/// freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (Stream != null)
Expand All @@ -79,11 +118,15 @@ public void Dispose()
}
}

/// <summary>
/// Extracts the contents of the zip file to the given directory.
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
public void ExtractToDirectory(string directoryName)
{
foreach (var entry in Entries.Where(e => !e.IsDirectory))
{
// create target directory with file folder
// create target directory for the file
var fileName = Path.Combine(directoryName, entry.Name);
var dirName = Path.GetDirectoryName(fileName);
Directory.CreateDirectory(dirName);
Expand All @@ -96,19 +139,30 @@ public void ExtractToDirectory(string directoryName)
}
}

/// <summary>
/// Extracts the specified file name.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="outputStream">The output stream.</param>
public void Extract(string fileName, Stream outputStream)
{
var entry = Entries.Where(e => e.Name == fileName).First();
Extract(entry, outputStream);
}

/// <summary>
/// Extracts the specified entry.
/// </summary>
/// <param name="entry">Zip file entry to extract.</param>
/// <param name="outputStream">The stream to write the data to.</param>
/// <exception cref="System.InvalidOperationException"> is thrown when the file header signature doesn't match.</exception>
public void Extract(Entry entry, Stream outputStream)
{
// check file signature
Stream.Seek(entry.HeaderOffset, SeekOrigin.Begin);
if (Reader.ReadInt32() != FileSignature)
{
throw new InvalidOperationException("File signature don't match.");
throw new InvalidOperationException("File signature doesn't match.");
}

// move to file data
Expand Down Expand Up @@ -139,6 +193,9 @@ public void Extract(Entry entry, Stream outputStream)
}
}

/// <summary>
/// Gets the file names.
/// </summary>
public IEnumerable<string> FileNames
{
get
Expand All @@ -149,6 +206,9 @@ public IEnumerable<string> FileNames

private Entry[] entries;

/// <summary>
/// Gets zip file entries.
/// </summary>
public IEnumerable<Entry> Entries
{
get
Expand Down
1 change: 1 addition & 0 deletions Unzip.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>bin\Debug\Unzip.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
4 changes: 2 additions & 2 deletions UsageExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Internals
{
struct Program
internal struct Program
{
static void Main(string[] args)
private static void Main(string[] args)
{
if (args.Length < 2)
{
Expand Down

0 comments on commit 6c80b71

Please sign in to comment.