/// <summary> /// General initialization function. /// </summary> /// <param name="archiveFullName">The archive file name.</param> private void Init(string archiveFullName) { _fileName = archiveFullName; bool isExecutable; _format = FileChecker.CheckSignature(archiveFullName, out _offset, out isExecutable); PreserveDirectoryStructure = true; SevenZipLibraryManager.LoadLibrary(this, _format); try { _archive = SevenZipLibraryManager.InArchive(_format, this); } catch (SevenZipLibraryException) { SevenZipLibraryManager.FreeLibrary(this, _format); throw; } if (isExecutable && _format != InArchiveFormat.PE) { if (!Check()) { CommonDispose(); _format = InArchiveFormat.PE; SevenZipLibraryManager.LoadLibrary(this, _format); try { _archive = SevenZipLibraryManager.InArchive(_format, this); } catch (SevenZipLibraryException) { SevenZipLibraryManager.FreeLibrary(this, _format); throw; } } } }
private static bool SpecialDetect(Stream stream, int offset, InArchiveFormat expectedFormat) { if (stream.Length > (long)(offset + 16)) { byte[] buffer = new byte[16]; int count = 16; int offset1 = 0; stream.Seek((long)offset, SeekOrigin.Begin); while (count > 0) { int num = stream.Read(buffer, offset1, count); count -= num; offset1 += num; } string str = BitConverter.ToString(buffer); foreach (string key in Formats.InSignatureFormats.Keys) { if (Formats.InSignatureFormats[key] == expectedFormat && str.StartsWith(key, StringComparison.OrdinalIgnoreCase)) { return(true); } } } return(false); }
private static bool SpecialDetect(Stream stream, int offset, InArchiveFormat expectedFormat) { if (stream.Length > offset + SIGNATURE_SIZE) { var signature = new byte[SIGNATURE_SIZE]; int bytesRequired = SIGNATURE_SIZE; int index = 0; stream.Seek(offset, SeekOrigin.Begin); while (bytesRequired > 0) { int bytesRead = stream.Read(signature, index, bytesRequired); bytesRequired -= bytesRead; index += bytesRead; } string actualSignature = BitConverter.ToString(signature); foreach (string expectedSignature in Formats.InSignatureFormats.Keys) { if (Formats.InSignatureFormats[expectedSignature] != expectedFormat) { continue; } if (actualSignature.StartsWith(expectedSignature, StringComparison.OrdinalIgnoreCase)) { return(true); } } } return(false); }
private static bool SpecialDetect(Stream stream, int offset, InArchiveFormat expectedFormat) { if (stream.Length > offset + SIGNATURE_SIZE) { var signature = new byte[SIGNATURE_SIZE]; int bytesRequired = SIGNATURE_SIZE; int index = 0; stream.Seek(offset, SeekOrigin.Begin); while (bytesRequired > 0) { int bytesRead = stream.Read(signature, index, bytesRequired); bytesRequired -= bytesRead; index += bytesRead; } string actualSignature = BitConverter.ToString(signature); foreach (string expectedSignature in Formats.InSignatureFormats.Keys) { if (Formats.InSignatureFormats[expectedSignature] != expectedFormat) { continue; } if (actualSignature.StartsWith(expectedSignature, StringComparison.OrdinalIgnoreCase)) { return true; } } } return false; }
/// <summary> /// Gets IInArchive interface to extract 7-zip archives. /// </summary> /// <param name = "format">Archive format.</param> /// <param name = "user">Archive format user.</param> public static IInArchive InArchive(InArchiveFormat format, object user) { #if !WINCE && !MONO lock (_libraryFileName) { #endif if (_inArchives[user][format] == null) { #if !WINCE && !MONO var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); sp.Demand(); if (_modulePtr == IntPtr.Zero) { LoadLibrary(user, format); if (_modulePtr == IntPtr.Zero) { throw new SevenZipLibraryException(); } } var createObject = (NativeMethods.CreateObjectDelegate) Marshal.GetDelegateForFunctionPointer( NativeMethods.GetProcAddress(_modulePtr, "CreateObject"), typeof(NativeMethods.CreateObjectDelegate)); if (createObject == null) { throw new SevenZipLibraryException(); } #endif object result; Guid interfaceId = #if !WINCE && !MONO typeof(IInArchive).GUID; #else new Guid(((GuidAttribute)typeof(IInArchive).GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value); #endif Guid classID = Formats.InFormatGuids[format]; try { #if !WINCE && !MONO createObject(ref classID, ref interfaceId, out result); #elif !MONO NativeMethods.CreateCOMObject(ref classID, ref interfaceId, out result); #else result = SevenZip.Mono.Factory.CreateInterface <IInArchive>(user, classID, interfaceId); #endif } catch (Exception) { throw new SevenZipLibraryException("Your 7-zip library does not support this archive type."); } InitUserInFormat(user, format); _inArchives[user][format] = result as IInArchive; } #if !WINCE && !MONO } #endif return(_inArchives[user][format]); }
internal PscxSevenZipExtractor(PscxCmdlet command, FileInfo file, bool passThru, InArchiveFormat format) : base(command, file, format) { _passThru = passThru; _entries = new List<ArchiveEntry>(); foreach (ArchiveEntry entry in this){ _entries.Add(entry); } }
private static void InitUserInFormat(object user, InArchiveFormat format) { if (!_inArchives.ContainsKey(user)) { _inArchives.Add(user, new Dictionary <InArchiveFormat, IInArchive>()); } if (!_inArchives[user].ContainsKey(format)) { _inArchives[user].Add(format, null); _totalUsers++; } }
private static void InitUserInFormat(object user, InArchiveFormat format) { if (!_inArchives.ContainsKey(user)) { _inArchives.Add(user, new Dictionary<InArchiveFormat, IInArchive>()); } if (!_inArchives[user].ContainsKey(format)) { _inArchives[user].Add(format, null); _totalUsers++; } }
/// <summary> /// Gets IInArchive interface to extract 7-zip archives. /// </summary> /// <param name="format">Archive format.</param> /// <param name="user">Archive format user.</param> public static IInArchive InArchive(InArchiveFormat format, object user) { lock (_syncRoot) { if (_inArchives[user][format] == null) { #if NET45 || NETSTANDARD2_0 var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); sp.Demand(); #endif if (_modulePtr == IntPtr.Zero) { LoadLibrary(user, format); if (_modulePtr == IntPtr.Zero) { throw new SevenZipLibraryException(); } } var createObject = (NativeMethods.CreateObjectDelegate) Marshal.GetDelegateForFunctionPointer( NativeMethods.GetProcAddress(_modulePtr, "CreateObject"), typeof(NativeMethods.CreateObjectDelegate)); if (createObject == null) { throw new SevenZipLibraryException(); } object result; var interfaceId = typeof(IInArchive).GUID; var classID = Formats.InFormatGuids[format]; try { createObject(ref classID, ref interfaceId, out result); } catch (Exception) { throw new SevenZipLibraryException("Your 7-zip library does not support this archive type."); } InitUserInFormat(user, format); _inArchives[user][format] = result as IInArchive; } return(_inArchives[user][format]); } }
internal ArchiveEntry(ArchiveFileInfo archiveEntry, string archivePath, InArchiveFormat archiveFormat) { Index = (uint) archiveEntry.Index; Path = archiveEntry.FileName; Size = archiveEntry.Size; Name = System.IO.Path.GetFileName(archiveEntry.FileName); CompressedSize = 0; // Not supported in SevenZipSharp (yet) ModifiedDate = archiveEntry.LastWriteTime; IsEncrypted = archiveEntry.Encrypted; IsFolder = archiveEntry.IsDirectory; ArchivePath = archivePath; Format = archiveFormat; CRC = archiveEntry.Crc; }
internal ArchiveEntry(ArchiveFileInfo archiveEntry, string archivePath, InArchiveFormat archiveFormat) { Index = (uint)archiveEntry.Index; Path = archiveEntry.FileName; Size = archiveEntry.Size; Name = System.IO.Path.GetFileName(archiveEntry.FileName); CompressedSize = 0; // Not supported in SevenZipSharp (yet) ModifiedDate = archiveEntry.LastWriteTime; IsEncrypted = archiveEntry.Encrypted; IsFolder = archiveEntry.IsDirectory; ArchivePath = archivePath; Format = archiveFormat; CRC = archiveEntry.Crc; }
private void Init(string archiveFullName) { this._FileName = archiveFullName; this._Format = FileChecker.CheckSignature(archiveFullName); SevenZipLibraryManager.LoadLibrary((object)this, (Enum)this._Format); try { this._Archive = SevenZipLibraryManager.InArchive(this._Format, (object)this); } catch (SevenZipLibraryException ex) { SevenZipLibraryManager.FreeLibrary((object)this, (Enum)this._Format); throw; } }
private void Init(Stream stream) { SevenZipExtractor.ValidateStream(stream); this._Format = FileChecker.CheckSignature(stream); SevenZipLibraryManager.LoadLibrary((object)this, (Enum)this._Format); try { this._InStream = stream; this._Archive = SevenZipLibraryManager.InArchive(this._Format, (object)this); this._PackedSize = new long?(stream.Length); } catch (SevenZipLibraryException ex) { SevenZipLibraryManager.FreeLibrary((object)this, (Enum)this._Format); throw; } }
protected SevenZipBaseEx(PscxCmdlet command, FileInfo file, InArchiveFormat format) { //Debug.Assert(format != ArchiveFormat.Unknown, "format != ArchiveFormat.Unknown"); _command = command; _file = file; _format = format; _archivePscxPath = PscxPathInfo.GetPscxPathInfo(_command.SessionState, file.FullName); string sevenZDll = PscxContext.Instance.Is64BitProcess ? "7z64.dll" : "7z.dll"; string sevenZPath = Path.Combine(PscxContext.Instance.Home, sevenZDll); Trace.Assert(File.Exists(sevenZPath), sevenZPath + " not found or inaccessible."); SevenZipBase.SetLibraryPath(sevenZPath); _extractor = new SevenZipExtractor(file.FullName); }
/// <summary> /// General initialization function. /// </summary> /// <param name="stream">The stream to read the archive from.</param> private void Init(Stream stream) { ValidateStream(stream); bool isExecutable = false; if ((int)_format == -1) { _format = FileChecker.CheckSignature(stream, out _offset, out isExecutable); } PreserveDirectoryStructure = true; SevenZipLibraryManager.LoadLibrary(this, _format); try { _inStream = new ArchiveEmulationStreamProxy(stream, _offset); _packedSize = stream.Length; _archive = SevenZipLibraryManager.InArchive(_format, this); } catch (SevenZipLibraryException) { SevenZipLibraryManager.FreeLibrary(this, _format); throw; } if (isExecutable && _format != InArchiveFormat.PE) { if (!Check()) { CommonDispose(); _format = InArchiveFormat.PE; try { _inStream = new ArchiveEmulationStreamProxy(stream, _offset); _packedSize = stream.Length; _archive = SevenZipLibraryManager.InArchive(_format, this); } catch (SevenZipLibraryException) { SevenZipLibraryManager.FreeLibrary(this, _format); throw; } } } }
/// <summary> /// Gets the InArchiveFormat for a specific file name. /// </summary> /// <param name="fileName">The archive file name.</param> /// <param name="offset">The archive beginning offset.</param> /// <param name="isExecutable">True if the original format of the file is PE; otherwise, false.</param> /// <returns>Corresponding InArchiveFormat.</returns> /// <exception cref="System.ArgumentException"/> public static InArchiveFormat CheckSignature(string fileName, out int offset, out bool isExecutable) { using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { InArchiveFormat format = CheckSignature(fs, out offset, out isExecutable); if (format != InArchiveFormat.None) { return(format); } } catch (ArgumentException) { } offset = 0; isExecutable = false; return(Formats.FormatByFileName(fileName, true)); } }
/// <summary> /// General initialization function. /// </summary> /// <param name="archiveFullName">The archive file name.</param> private void Init(string archiveFullName) { _fileName = archiveFullName; bool isExecutable = false; if ((int)_format == -1) { _format = FileChecker.CheckSignature(archiveFullName, out _offset, out isExecutable); } PreserveDirectoryStructure = true; SevenZipLibraryManager.LoadLibrary(this, _format); try { _archive = SevenZipLibraryManager.InArchive(_format, this); } catch (SevenZipLibraryException) { SevenZipLibraryManager.FreeLibrary(this, _format); throw; } if (isExecutable && _format != InArchiveFormat.PE) { if (!Check()) { CommonDispose(); _format = InArchiveFormat.PE; SevenZipLibraryManager.LoadLibrary(this, _format); try { _archive = SevenZipLibraryManager.InArchive(_format, this); } catch (SevenZipLibraryException) { SevenZipLibraryManager.FreeLibrary(this, _format); throw; } } } }
/// <summary> /// Gets the InArchiveFormat for a specific extension. /// </summary> /// <param name="stream">The stream to identify.</param> /// <param name="offset">The archive beginning offset.</param> /// <param name="isExecutable">True if the original format of the stream is PE; otherwise, false.</param> /// <returns>Corresponding InArchiveFormat.</returns> public static InArchiveFormat CheckSignature(Stream stream, out int offset, out bool isExecutable) { offset = 0; if (!stream.CanRead) { throw new ArgumentException("The stream must be readable."); } if (stream.Length < SIGNATURE_SIZE) { throw new ArgumentException("The stream is invalid."); } #region Get file signature var signature = new byte[SIGNATURE_SIZE]; int bytesRequired = SIGNATURE_SIZE; int index = 0; stream.Seek(0, SeekOrigin.Begin); while (bytesRequired > 0) { int bytesRead = stream.Read(signature, index, bytesRequired); bytesRequired -= bytesRead; index += bytesRead; } string actualSignature = BitConverter.ToString(signature); #endregion InArchiveFormat suspectedFormat = InArchiveFormat.XZ; // any except PE and Cab isExecutable = false; foreach (string expectedSignature in Formats.InSignatureFormats.Keys) { if (actualSignature.StartsWith(expectedSignature, StringComparison.OrdinalIgnoreCase) || actualSignature.Substring(6).StartsWith(expectedSignature, StringComparison.OrdinalIgnoreCase) && Formats.InSignatureFormats[expectedSignature] == InArchiveFormat.Lzh) { if (Formats.InSignatureFormats[expectedSignature] == InArchiveFormat.PE) { suspectedFormat = InArchiveFormat.PE; isExecutable = true; } else { return(Formats.InSignatureFormats[expectedSignature]); } } } // Many Microsoft formats if (actualSignature.StartsWith("D0-CF-11-E0-A1-B1-1A-E1", StringComparison.OrdinalIgnoreCase)) { suspectedFormat = InArchiveFormat.Cab; // != InArchiveFormat.XZ } #region SpecialDetect try { SpecialDetect(stream, 257, InArchiveFormat.Tar); } catch (ArgumentException) {} if (SpecialDetect(stream, 0x8001, InArchiveFormat.Iso)) { return(InArchiveFormat.Iso); } if (SpecialDetect(stream, 0x8801, InArchiveFormat.Iso)) { return(InArchiveFormat.Iso); } if (SpecialDetect(stream, 0x9001, InArchiveFormat.Iso)) { return(InArchiveFormat.Iso); } if (SpecialDetect(stream, 0x9001, InArchiveFormat.Iso)) { return(InArchiveFormat.Iso); } if (SpecialDetect(stream, 0x400, InArchiveFormat.Hfs)) { return(InArchiveFormat.Hfs); } #region Last resort for tar - can mistake if (stream.Length >= 1024) { stream.Seek(-1024, SeekOrigin.End); byte[] buf = new byte[1024]; stream.Read(buf, 0, 1024); bool istar = true; for (int i = 0; i < 1024; i++) { istar = istar && buf[i] == 0; } if (istar) { return(InArchiveFormat.Tar); } } #endregion #endregion #region Check if it is an SFX archive or a file with an embedded archive. if (suspectedFormat != InArchiveFormat.XZ) { #region Get first Min(stream.Length, SFX_SCAN_LENGTH) bytes var scanLength = Math.Min(stream.Length, SFX_SCAN_LENGTH); signature = new byte[scanLength]; bytesRequired = (int)scanLength; index = 0; stream.Seek(0, SeekOrigin.Begin); while (bytesRequired > 0) { int bytesRead = stream.Read(signature, index, bytesRequired); bytesRequired -= bytesRead; index += bytesRead; } actualSignature = BitConverter.ToString(signature); #endregion foreach (var format in new InArchiveFormat[] { InArchiveFormat.Zip, InArchiveFormat.KCompress, InArchiveFormat.Rar, InArchiveFormat.Cab, InArchiveFormat.Arj }) { int pos = actualSignature.IndexOf(Formats.InSignatureFormatsReversed[format]); if (pos > -1) { offset = pos / 3; return(format); } } // Nothing if (suspectedFormat == InArchiveFormat.PE) { return(InArchiveFormat.PE); } } #endregion throw new ArgumentException("The stream is invalid or no corresponding signature was found."); }
/// <summary> /// Initializes a new instance of SevenZipExtractor class. /// </summary> /// <param name="archiveFullName">The archive full file name.</param> /// <param name="format">Manual archive format setup. You SHOULD NOT normally specify it this way. /// Instead, use SevenZipExtractor(string archiveFullName), that constructor /// automatically detects the archive format.</param> public SevenZipExtractor(string archiveFullName, InArchiveFormat format) { _format = format; Init(archiveFullName); }
/// <summary> /// Initializes a new instance of SevenZipExtractor class. /// </summary> /// <param name="archiveStream">The stream to read the archive from.</param> /// <param name="password">Password for an encrypted archive.</param> /// <param name="format">Manual archive format setup. You SHOULD NOT normally specify it this way. /// Instead, use SevenZipExtractor(Stream archiveStream, string password), that constructor /// automatically detects the archive format.</param> public SevenZipExtractor(Stream archiveStream, string password, InArchiveFormat format) : base(password) { _format = format; Init(archiveStream); }
/// <summary> /// Initializes a new instance of SevenZipExtractor class. /// </summary> /// <param name="archiveFullName">The archive full file name.</param> /// <param name="password">Password for an encrypted archive.</param> /// <param name="format">Manual archive format setup. You SHOULD NOT normally specify it this way. /// Instead, use SevenZipExtractor(string archiveFullName, string password), that constructor /// automatically detects the archive format.</param> public SevenZipExtractor(string archiveFullName, string password, InArchiveFormat format) : base(password) { _format = format; Init(archiveFullName); }
/// <summary> /// Initializes a new instance of SevenZipExtractor class. /// </summary> /// <param name="archiveStream">The stream to read the archive from. /// Use SevenZipExtractor(string) to extract from disk, though it is not necessary.</param> /// <param name="format">Manual archive format setup. You SHOULD NOT normally specify it this way. /// Instead, use SevenZipExtractor(Stream archiveStream), that constructor /// automatically detects the archive format.</param> public SevenZipExtractor(Stream archiveStream, InArchiveFormat format) { _format = format; Init(archiveStream); }
/// <summary> /// Gets IInArchive interface to extract 7-zip archives. /// </summary> /// <param name="format">Archive format.</param> /// <param name="user">Archive format user.</param> public static IInArchive InArchive(InArchiveFormat format, object user) { lock (_syncRoot) { if (_inArchives[user][format] == null) { #if !WINCE && !MONO var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); sp.Demand(); if (_modulePtr == IntPtr.Zero) { LoadLibrary(user, format); if (_modulePtr == IntPtr.Zero) { throw new SevenZipLibraryException(); } } var createObject = (NativeMethods.CreateObjectDelegate) Marshal.GetDelegateForFunctionPointer( NativeMethods.GetProcAddress(_modulePtr, "CreateObject"), typeof(NativeMethods.CreateObjectDelegate)); if (createObject == null) { throw new SevenZipLibraryException(); } #endif object result; Guid interfaceId = #if !WINCE && !MONO typeof(IInArchive).GUID; #else new Guid(((GuidAttribute)typeof(IInArchive).GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value); #endif Guid classID = Formats.InFormatGuids[format]; try { #if !WINCE && !MONO createObject(ref classID, ref interfaceId, out result); #elif !MONO NativeMethods.CreateCOMObject(ref classID, ref interfaceId, out result); #else result = SevenZip.Mono.Factory.CreateInterface<IInArchive>(user, classID, interfaceId); #endif } catch (Exception) { throw new SevenZipLibraryException("Your 7-zip library does not support this archive type."); } InitUserInFormat(user, format); _inArchives[user][format] = result as IInArchive; } return _inArchives[user][format]; #if !WINCE && !MONO } #endif }
internal PscxSevenZipReader(PscxCmdlet command, FileInfo file, InArchiveFormat format) : base(command, file, format) { command.WriteDebug(String.Format("Created {0} reader for {1}.", format, file)); }
internal PscxSevenZipExtractor(PscxCmdlet command, FileInfo file, bool passThru, InArchiveFormat format) : base(command, file, format) { _passThru = passThru; _entries = new List <ArchiveEntry>(); foreach (ArchiveEntry entry in this) { _entries.Add(entry); } }
public FileCheckerTestData(string testDataFilePath, InArchiveFormat expectedFormat) { TestDataFilePath = testDataFilePath; ExpectedFormat = expectedFormat; }