-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -13,7 +14,7 @@ namespace Internals | |
/// <summary> | ||
/// Unzip helper class. | ||
/// </summary> | ||
public class Unzip : IDisposable | ||
internal class Unzip : IDisposable | ||
{ | ||
private const int EntrySignature = 0x02014B50; | ||
|
||
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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); | ||
|
@@ -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 | ||
|
@@ -139,6 +193,9 @@ public void Extract(Entry entry, Stream outputStream) | |
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the file names. | ||
/// </summary> | ||
public IEnumerable<string> FileNames | ||
{ | ||
get | ||
|
@@ -149,6 +206,9 @@ public IEnumerable<string> FileNames | |
|
||
private Entry[] entries; | ||
|
||
/// <summary> | ||
/// Gets zip file entries. | ||
/// </summary> | ||
public IEnumerable<Entry> Entries | ||
{ | ||
get | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters