📄 zipfile.cs
字号:
// ZipFile.cs
//
// Copyright (C) 2001 Mike Krueger
// Copyright (C) 2004 John Reilly
//
// This file was translated from java, it was part of the GNU Classpath
// Copyright (C) 2001 Free Software Foundation, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Linking this library statically or dynamically with other modules is
// making a combined work based on this library. Thus, the terms and
// conditions of the GNU General Public License cover the whole
// combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent
// modules, and to copy and distribute the resulting executable under
// terms of your choice, provided that you also meet, for each linked
// independent module, the terms and conditions of the license of that
// module. An independent module is a module which is not derived from
// or based on this library. If you modify this library, you may extend
// this exception to your version of the library, but you are not
// obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
using System;
using System.Security.Cryptography;
using System.Collections;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Encryption;
namespace ICSharpCode.SharpZipLib.Zip
{
/// <summary>
/// Arguments used with KeysRequiredEvent
/// </summary>
public class KeysRequiredEventArgs : EventArgs
{
string fileName;
/// <summary>
/// Get the name of the file for which keys are required.
/// </summary>
public string FileName
{
get { return fileName; }
}
byte[] key;
/// <summary>
/// Get/set the key value
/// </summary>
public byte[] Key
{
get { return key; }
set { key = value; }
}
/// <summary>
/// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
/// </summary>
/// <param name="name">The name of the file for which keys are required.</param>
public KeysRequiredEventArgs(string name)
{
fileName = name;
}
/// <summary>
/// Initialise a new instance of <see cref="KeysRequiredEventArgs"></see>
/// </summary>
/// <param name="name">The name of the file for which keys are required.</param>
/// <param name="keyValue">The current key value.</param>
public KeysRequiredEventArgs(string name, byte[] keyValue)
{
fileName = name;
key = keyValue;
}
}
/// <summary>
/// This class represents a Zip archive. You can ask for the contained
/// entries, or get an input stream for a file entry. The entry is
/// automatically decompressed.
///
/// This class is thread safe: You can open input streams for arbitrary
/// entries in different threads.
/// <br/>
/// <br/>Author of the original java version : Jochen Hoenicke
/// </summary>
/// <example>
/// <code>
/// using System;
/// using System.Text;
/// using System.Collections;
/// using System.IO;
///
/// using ICSharpCode.SharpZipLib.Zip;
///
/// class MainClass
/// {
/// static public void Main(string[] args)
/// {
/// ZipFile zFile = new ZipFile(args[0]);
/// Console.WriteLine("Listing of : " + zFile.Name);
/// Console.WriteLine("");
/// Console.WriteLine("Raw Size Size Date Time Name");
/// Console.WriteLine("-------- -------- -------- ------ ---------");
/// foreach (ZipEntry e in zFile) {
/// DateTime d = e.DateTime;
/// Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
/// d.ToString("dd-MM-yy"), d.ToString("t"),
/// e.Name);
/// }
/// }
/// }
/// </code>
/// </example>
public class ZipFile : IEnumerable
{
string name;
string comment;
Stream baseStream;
bool isStreamOwner = true;
long offsetOfFirstEntry = 0;
ZipEntry[] entries;
#region KeyHandling
/// <summary>
/// Delegate for handling keys/password setting during compresion/decompression.
/// </summary>
public delegate void KeysRequiredEventHandler(
object sender,
KeysRequiredEventArgs e
);
/// <summary>
/// Event handler for handling encryption keys.
/// </summary>
public KeysRequiredEventHandler KeysRequired;
/// <summary>
/// Handles getting of encryption keys when required.
/// </summary>
/// <param name="fileName">The file for which encryptino keys are required.</param>
void OnKeysRequired(string fileName)
{
if (KeysRequired != null) {
KeysRequiredEventArgs krea = new KeysRequiredEventArgs(fileName, key);
KeysRequired(this, krea);
key = krea.Key;
}
}
byte[] key = null;
/// <summary>
/// Get/set the encryption key value.
/// </summary>
byte[] Key
{
get { return key; }
set { key = value; }
}
/// <summary>
/// Password to be used for encrypting/decrypting files.
/// </summary>
/// <remarks>Set to null if no password is required.</remarks>
public string Password
{
set
{
if ( (value == null) || (value.Length == 0) ) {
key = null;
}
else {
key = PkzipClassic.GenerateKeys(Encoding.ASCII.GetBytes(value));
}
}
}
byte[] iv = null;
bool HaveKeys
{
get { return key != null; }
}
#endregion
/// <summary>
/// Opens a Zip file with the given name for reading.
/// </summary>
/// <exception cref="IOException">
/// An i/o error occurs
/// </exception>
/// <exception cref="ZipException">
/// The file doesn't contain a valid zip archive.
/// </exception>
public ZipFile(string name)
{
this.name = name;
this.baseStream = File.OpenRead(name);
try {
ReadEntries();
}
catch {
Close();
throw;
}
}
/// <summary>
/// Opens a Zip file reading the given FileStream
/// </summary>
/// <exception cref="IOException">
/// An i/o error occurs.
/// </exception>
/// <exception cref="ZipException">
/// The file doesn't contain a valid zip archive.
/// </exception>
public ZipFile(FileStream file)
{
this.baseStream = file;
this.name = file.Name;
try {
ReadEntries();
}
catch {
Close();
throw;
}
}
/// <summary>
/// Opens a Zip file reading the given Stream
/// </summary>
/// <exception cref="IOException">
/// An i/o error occurs
/// </exception>
/// <exception cref="ZipException">
/// The file doesn't contain a valid zip archive.<br/>
/// The stream provided cannot seek
/// </exception>
public ZipFile(Stream baseStream)
{
this.baseStream = baseStream;
this.name = null;
try {
ReadEntries();
}
catch {
Close();
throw;
}
}
/// <summary>
/// Get/set a flag indicating if the underlying stream is owned by the ZipFile instance.
/// If the flag is true then the stream will be closed when <see cref="Close">Close</see> is called.
/// </summary>
/// <remarks>
/// The default value is true in all cases.
/// </remarks>
bool IsStreamOwner
{
get { return isStreamOwner; }
set { isStreamOwner = value; }
}
/// <summary>
/// Read an unsigned short in little endian byte order.
/// </summary>
/// <returns>Returns the value read.</returns>
/// <exception cref="IOException">
/// An i/o error occurs.
/// </exception>
/// <exception cref="EndOfStreamException">
/// The file ends prematurely
/// </exception>
int ReadLeShort()
{
return baseStream.ReadByte() | baseStream.ReadByte() << 8;
}
/// <summary>
/// Read an int in little endian byte order.
/// </summary>
/// <returns>Returns the value read.</returns>
/// <exception cref="IOException">
/// An i/o error occurs.
/// </exception>
/// <exception cref="System.IO.EndOfStreamException">
/// The file ends prematurely
/// </exception>
int ReadLeInt()
{
return ReadLeShort() | ReadLeShort() << 16;
}
// NOTE this returns the offset of the first byte after the signature.
long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData)
{
long pos = endLocation - minimumBlockSize;
if (pos < 0) {
return -1;
}
long giveUpMarker = Math.Max(pos - maximumVariableData, 0);
// TODO: this loop could be optimised for speed.
do
{
if (pos < giveUpMarker) {
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -