/* This file is part of SevenZipSharp.
SevenZipSharp is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SevenZipSharp 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SevenZipSharp. If not, see .
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace SevenZip
{
#if UNMANAGED
///
/// Readable archive format enumeration.
///
public enum InArchiveFormat
{
///
/// Open 7-zip archive format.
///
/// Wikipedia information
SevenZip,
///
/// Proprietary Arj archive format.
///
/// Wikipedia information
Arj,
///
/// Open Bzip2 archive format.
///
/// Wikipedia information
BZip2,
///
/// Microsoft cabinet archive format.
///
/// Wikipedia information
Cab,
///
/// Microsoft Compiled HTML Help file format.
///
/// Wikipedia information
Chm,
///
/// Microsoft Compound file format.
///
/// Wikipedia information
Compound,
///
/// Open Cpio archive format.
///
/// Wikipedia information
Cpio,
///
/// Open Debian software package format.
///
/// Wikipedia information
Deb,
///
/// Open Gzip archive format.
///
/// Wikipedia information
GZip,
///
/// Open ISO disk image format.
///
/// Wikipedia information
Iso,
///
/// Open Lzh archive format.
///
/// Wikipedia information
Lzh,
///
/// Open core 7-zip Lzma raw archive format.
///
/// Wikipedia information
Lzma,
///
/// Nullsoft installation package format.
///
/// Wikipedia information
Nsis,
///
/// RarLab Rar archive format.
///
/// Wikipedia information
Rar,
///
/// Open Rpm software package format.
///
/// Wikipedia information
Rpm,
///
/// Open split file format.
///
/// Wikipedia information
Split,
///
/// Open Tar archive format.
///
/// Wikipedia information
Tar,
///
/// Microsoft Windows Imaging disk image format.
///
/// Wikipedia information
Wim,
///
/// Open LZW archive format; implemented in "compress" program; also known as "Z" archive format.
///
/// Wikipedia information
Lzw,
///
/// Open Zip archive format.
///
/// Wikipedia information
Zip,
///
/// Open Udf disk image format.
///
Udf,
///
/// Xar open source archive format.
///
/// Wikipedia information
Xar,
///
/// Mub
///
Mub,
///
/// Macintosh Disk Image on CD.
///
/// Wikipedia information
Hfs,
///
/// Apple Mac OS X Disk Copy Disk Image format.
///
Dmg,
///
/// Open Xz archive format.
///
/// Wikipedia information
XZ,
///
/// MSLZ archive format.
///
Mslz,
///
/// Flash video format.
///
/// Wikipedia information
Flv,
///
/// Shockwave Flash format.
///
/// Wikipedia information
Swf,
///
/// Windows PE executable format.
///
/// Wikipedia information
PE,
///
/// Linux executable Elf format.
///
/// Wikipedia information
Elf,
///
/// Windows Installer Database.
///
/// Wikipedia information
Msi,
///
/// Microsoft virtual hard disk file format.
///
/// Wikipedia information
Vhd,
///
/// Not an archive
///
None
}
#if COMPRESS
///
/// Writable archive format enumeration.
///
public enum OutArchiveFormat
{
///
/// Open 7-zip archive format.
///
/// Wikipedia information
SevenZip,
///
/// Open Zip archive format.
///
/// Wikipedia information
Zip,
///
/// Open Gzip archive format.
///
/// Wikipedia information
GZip,
///
/// Open Bzip2 archive format.
///
/// Wikipedia information
BZip2,
///
/// Open Tar archive format.
///
/// Wikipedia information
Tar,
///
/// Open Xz archive format.
///
/// Wikipedia information
XZ
}
///
/// Compression level enumeration
///
public enum CompressionLevel
{
///
/// No compression
///
None,
///
/// Very low compression level
///
Fast,
///
/// Low compression level
///
Low,
///
/// Normal compression level (default)
///
Normal,
///
/// High compression level
///
High,
///
/// The best compression level (slow)
///
Ultra
}
///
/// Compression method enumeration.
///
/// Some methods are applicable only to Zip format, some - only to 7-zip.
public enum CompressionMethod
{
///
/// Zip or 7-zip|no compression method.
///
Copy,
///
/// Zip|Deflate method.
///
Deflate,
///
/// Zip|Deflate64 method.
///
Deflate64,
///
/// Zip or 7-zip|Bzip2 method.
///
/// Wikipedia information
BZip2,
///
/// Zip or 7-zip|LZMA method based on Lempel-Ziv algorithm, it is default for 7-zip.
///
Lzma,
///
/// 7-zip|LZMA version 2, LZMA with improved multithreading and usually slight archive size decrease.
///
Lzma2,
///
/// Zip or 7-zip|PPMd method based on Dmitry Shkarin's PPMdH source code, very efficient for compressing texts.
///
/// Wikipedia information
Ppmd,
///
/// No method change.
///
Default
}
#endif
///
/// Archive format routines
///
public static class Formats
{
/*///
/// Gets the max value of the specified enum type.
///
/// Type of the enum
/// Max value
internal static int GetMaxValue(Type type)
{
List enumList = new List((IEnumerable)Enum.GetValues(type));
enumList.Sort();
return enumList[enumList.Count - 1];
}*/
///
/// List of readable archive format interface guids for 7-zip COM interop.
///
internal static readonly Dictionary InFormatGuids =
new Dictionary(20)
#region InFormatGuids initialization
{
{InArchiveFormat.SevenZip, new Guid("23170f69-40c1-278a-1000-000110070000")},
{InArchiveFormat.Arj, new Guid("23170f69-40c1-278a-1000-000110040000")},
{InArchiveFormat.BZip2, new Guid("23170f69-40c1-278a-1000-000110020000")},
{InArchiveFormat.Cab, new Guid("23170f69-40c1-278a-1000-000110080000")},
{InArchiveFormat.Chm, new Guid("23170f69-40c1-278a-1000-000110e90000")},
{InArchiveFormat.Compound, new Guid("23170f69-40c1-278a-1000-000110e50000")},
{InArchiveFormat.Cpio, new Guid("23170f69-40c1-278a-1000-000110ed0000")},
{InArchiveFormat.Deb, new Guid("23170f69-40c1-278a-1000-000110ec0000")},
{InArchiveFormat.GZip, new Guid("23170f69-40c1-278a-1000-000110ef0000")},
{InArchiveFormat.Iso, new Guid("23170f69-40c1-278a-1000-000110e70000")},
{InArchiveFormat.Lzh, new Guid("23170f69-40c1-278a-1000-000110060000")},
{InArchiveFormat.Lzma, new Guid("23170f69-40c1-278a-1000-0001100a0000")},
{InArchiveFormat.Nsis, new Guid("23170f69-40c1-278a-1000-000110090000")},
{InArchiveFormat.Rar, new Guid("23170f69-40c1-278a-1000-000110030000")},
{InArchiveFormat.Rpm, new Guid("23170f69-40c1-278a-1000-000110eb0000")},
{InArchiveFormat.Split, new Guid("23170f69-40c1-278a-1000-000110ea0000")},
{InArchiveFormat.Tar, new Guid("23170f69-40c1-278a-1000-000110ee0000")},
{InArchiveFormat.Wim, new Guid("23170f69-40c1-278a-1000-000110e60000")},
{InArchiveFormat.Lzw, new Guid("23170f69-40c1-278a-1000-000110050000")},
{InArchiveFormat.Zip, new Guid("23170f69-40c1-278a-1000-000110010000")},
{InArchiveFormat.Udf, new Guid("23170f69-40c1-278a-1000-000110E00000")},
{InArchiveFormat.Xar, new Guid("23170f69-40c1-278a-1000-000110E10000")},
{InArchiveFormat.Mub, new Guid("23170f69-40c1-278a-1000-000110E20000")},
{InArchiveFormat.Hfs, new Guid("23170f69-40c1-278a-1000-000110E30000")},
{InArchiveFormat.Dmg, new Guid("23170f69-40c1-278a-1000-000110E40000")},
{InArchiveFormat.XZ, new Guid("23170f69-40c1-278a-1000-0001100C0000")},
{InArchiveFormat.Mslz, new Guid("23170f69-40c1-278a-1000-000110D50000")},
{InArchiveFormat.PE, new Guid("23170f69-40c1-278a-1000-000110DD0000")},
{InArchiveFormat.Elf, new Guid("23170f69-40c1-278a-1000-000110DE0000")},
{InArchiveFormat.Swf, new Guid("23170f69-40c1-278a-1000-000110D70000")},
{InArchiveFormat.Vhd, new Guid("23170f69-40c1-278a-1000-000110DC0000")}
};
#endregion
#if COMPRESS
///
/// List of writable archive format interface guids for 7-zip COM interop.
///
internal static readonly Dictionary OutFormatGuids =
new Dictionary(2)
#region OutFormatGuids initialization
{
{OutArchiveFormat.SevenZip, new Guid("23170f69-40c1-278a-1000-000110070000")},
{OutArchiveFormat.Zip, new Guid("23170f69-40c1-278a-1000-000110010000")},
{OutArchiveFormat.BZip2, new Guid("23170f69-40c1-278a-1000-000110020000")},
{OutArchiveFormat.GZip, new Guid("23170f69-40c1-278a-1000-000110ef0000")},
{OutArchiveFormat.Tar, new Guid("23170f69-40c1-278a-1000-000110ee0000")},
{OutArchiveFormat.XZ, new Guid("23170f69-40c1-278a-1000-0001100C0000")},
};
#endregion
internal static readonly Dictionary MethodNames =
new Dictionary(6)
#region MethodNames initialization
{
{CompressionMethod.Copy, "Copy"},
{CompressionMethod.Deflate, "Deflate"},
{CompressionMethod.Deflate64, "Deflate64"},
{CompressionMethod.Lzma, "LZMA"},
{CompressionMethod.Lzma2, "LZMA2"},
{CompressionMethod.Ppmd, "PPMd"},
{CompressionMethod.BZip2, "BZip2"}
};
#endregion
internal static readonly Dictionary InForOutFormats =
new Dictionary(6)
#region InForOutFormats initialization
{
{OutArchiveFormat.SevenZip, InArchiveFormat.SevenZip},
{OutArchiveFormat.GZip, InArchiveFormat.GZip},
{OutArchiveFormat.BZip2, InArchiveFormat.BZip2},
{OutArchiveFormat.Tar, InArchiveFormat.Tar},
{OutArchiveFormat.XZ, InArchiveFormat.XZ},
{OutArchiveFormat.Zip, InArchiveFormat.Zip}
};
#endregion
#endif
///
/// List of archive formats corresponding to specific extensions
///
private static readonly Dictionary InExtensionFormats =
new Dictionary
#region InExtensionFormats initialization
{{"7z", InArchiveFormat.SevenZip},
{"gz", InArchiveFormat.GZip},
{"tar", InArchiveFormat.Tar},
{"rar", InArchiveFormat.Rar},
{"zip", InArchiveFormat.Zip},
{"lzma", InArchiveFormat.Lzma},
{"lzh", InArchiveFormat.Lzh},
{"arj", InArchiveFormat.Arj},
{"bz2", InArchiveFormat.BZip2},
{"cab", InArchiveFormat.Cab},
{"chm", InArchiveFormat.Chm},
{"deb", InArchiveFormat.Deb},
{"iso", InArchiveFormat.Iso},
{"rpm", InArchiveFormat.Rpm},
{"wim", InArchiveFormat.Wim},
{"udf", InArchiveFormat.Udf},
{"mub", InArchiveFormat.Mub},
{"xar", InArchiveFormat.Xar},
{"hfs", InArchiveFormat.Hfs},
{"dmg", InArchiveFormat.Dmg},
{"Z", InArchiveFormat.Lzw},
{"xz", InArchiveFormat.XZ},
{"flv", InArchiveFormat.Flv},
{"swf", InArchiveFormat.Swf},
{"exe", InArchiveFormat.PE},
{"dll", InArchiveFormat.PE},
{"vhd", InArchiveFormat.Vhd}
};
#endregion
///
/// List of archive formats corresponding to specific signatures
///
/// Based on the information at this site.
internal static readonly Dictionary InSignatureFormats =
new Dictionary
#region InSignatureFormats initialization
{{"37-7A-BC-AF-27-1C", InArchiveFormat.SevenZip},
{"1F-8B-08", InArchiveFormat.GZip},
{"75-73-74-61-72", InArchiveFormat.Tar},
//257 byte offset
{"52-61-72-21-1A-07-00", InArchiveFormat.Rar},
{"50-4B-03-04", InArchiveFormat.Zip},
{"5D-00-00-40-00", InArchiveFormat.Lzma},
{"2D-6C-68", InArchiveFormat.Lzh},
//^ 2 byte offset
{"1F-9D-90", InArchiveFormat.Lzw},
{"60-EA", InArchiveFormat.Arj},
{"42-5A-68", InArchiveFormat.BZip2},
//{"4D-53-43-46", InArchiveFormat.Cab},
//{"49-54-53-46", InArchiveFormat.Chm},
//{"21-3C-61-72-63-68-3E-0A-64-65-62-69-61-6E-2D-62-69-6E-61-72-79", InArchiveFormat.Deb},
//{"43-44-30-30-31", InArchiveFormat.Iso},
//^ 0x8001, 0x8801 or 0x9001 byte offset
//{"ED-AB-EE-DB", InArchiveFormat.Rpm},
//{"4D-53-57-49-4D-00-00-00", InArchiveFormat.Wim},
//{"udf", InArchiveFormat.Udf},
//{"mub", InArchiveFormat.Mub},
//{"78-61-72-21", InArchiveFormat.Xar},
//0x400 byte offset
//{"48-2B", InArchiveFormat.Hfs},
{"FD-37-7A-58-5A", InArchiveFormat.XZ},
//{"46-4C-56", InArchiveFormat.Flv},
//{"46-57-53", InArchiveFormat.Swf},
//{"4D-5A", InArchiveFormat.PE},
//{"7F-45-4C-46", InArchiveFormat.Elf},
//{"78", InArchiveFormat.Dmg},
//{"63-6F-6E-65-63-74-69-78", InArchiveFormat.Vhd},
{"4E-45-53", InArchiveFormat.None}
};
#endregion
internal static Dictionary InSignatureFormatsReversed;
static Formats()
{
InSignatureFormatsReversed = new Dictionary(InSignatureFormats.Count);
foreach (var pair in InSignatureFormats)
{
InSignatureFormatsReversed.Add(pair.Value, pair.Key);
}
}
///
/// Gets InArchiveFormat for specified archive file name
///
/// Archive file name
/// Indicates whether to throw exceptions
/// InArchiveFormat recognized by the file name extension
///
public static InArchiveFormat FormatByFileName(string fileName, bool reportErrors)
{
if (String.IsNullOrEmpty(fileName) && reportErrors)
{
throw new ArgumentException("File name is null or empty string!");
}
string extension = Path.GetExtension(fileName);
if (extension.StartsWith("."))
extension = extension.Substring(1);
if (!InExtensionFormats.ContainsKey(extension) && reportErrors)
{
if (FileChecker.ThrowExceptions)
throw new ArgumentException("Extension \"" + extension + "\" is not a supported archive file name extension.");
else return InArchiveFormat.None;
}
return InExtensionFormats[extension];
}
}
#endif
}