123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- namespace SevenZip
- {
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.InteropServices;
- #if UNMANAGED
-
-
-
- internal sealed class ArchiveOpenCallback : CallbackBase, IArchiveOpenCallback, IArchiveOpenVolumeCallback,
- ICryptoGetTextPassword, IDisposable
- {
- private FileInfo _fileInfo;
- private Dictionary<string, InStreamWrapper> _wrappers =
- new Dictionary<string, InStreamWrapper>();
- private readonly List<string> _volumeFileNames = new List<string>();
-
-
-
- public IList<string> VolumeFileNames => _volumeFileNames;
-
-
-
-
- private void Init(string fileName)
- {
- if (!string.IsNullOrEmpty(fileName))
- {
- _fileInfo = new FileInfo(fileName);
- _volumeFileNames.Add(fileName);
- if (fileName.EndsWith("001"))
- {
- int index = 2;
- var baseName = fileName.Substring(0, fileName.Length - 3);
- var volName = baseName + (index > 99 ? index.ToString() :
- index > 9 ? "0" + index : "00" + index);
- while (File.Exists(volName))
- {
- _volumeFileNames.Add(volName);
- index++;
- volName = baseName + (index > 99 ? index.ToString() :
- index > 9 ? "0" + index : "00" + index);
- }
- }
- }
- }
-
-
-
-
- public ArchiveOpenCallback(string fileName)
- {
- Init(fileName);
- }
-
-
-
-
-
- public ArchiveOpenCallback(string fileName, string password) : base(password)
- {
- Init(fileName);
- }
- #region IArchiveOpenCallback Members
- public void SetTotal(IntPtr files, IntPtr bytes) {}
- public void SetCompleted(IntPtr files, IntPtr bytes) {}
- #endregion
- #region IArchiveOpenVolumeCallback Members
- public int GetProperty(ItemPropId propId, ref PropVariant value)
- {
- if (_fileInfo == null)
- {
-
- return 0;
- }
- switch (propId)
- {
- case ItemPropId.Name:
- value.VarType = VarEnum.VT_BSTR;
- value.Value = Marshal.StringToBSTR(_fileInfo.FullName);
- break;
- case ItemPropId.IsDirectory:
- value.VarType = VarEnum.VT_BOOL;
- value.UInt64Value = (byte) (_fileInfo.Attributes & FileAttributes.Directory);
- break;
- case ItemPropId.Size:
- value.VarType = VarEnum.VT_UI8;
- value.UInt64Value = (ulong) _fileInfo.Length;
- break;
- case ItemPropId.Attributes:
- value.VarType = VarEnum.VT_UI4;
- value.UInt32Value = (uint) _fileInfo.Attributes;
- break;
- case ItemPropId.CreationTime:
- value.VarType = VarEnum.VT_FILETIME;
- value.Int64Value = _fileInfo.CreationTime.ToFileTime();
- break;
- case ItemPropId.LastAccessTime:
- value.VarType = VarEnum.VT_FILETIME;
- value.Int64Value = _fileInfo.LastAccessTime.ToFileTime();
- break;
- case ItemPropId.LastWriteTime:
- value.VarType = VarEnum.VT_FILETIME;
- value.Int64Value = _fileInfo.LastWriteTime.ToFileTime();
- break;
- }
- return 0;
- }
- public int GetStream(string name, out IInStream inStream)
- {
- if (!File.Exists(name))
- {
- name = Path.Combine(Path.GetDirectoryName(_fileInfo.FullName), name);
- if (!File.Exists(name))
- {
- inStream = null;
- AddException(new FileNotFoundException("The volume \"" + name + "\" was not found. Extraction can be impossible."));
- return 1;
- }
- }
- _volumeFileNames.Add(name);
- if (_wrappers.ContainsKey(name))
- {
- _wrappers[name].Seek(0, SeekOrigin.Begin, IntPtr.Zero);
- inStream = _wrappers[name];
- }
- else
- {
- try
- {
- var wrapper = new InStreamWrapper(
- new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true);
- _wrappers.Add(name, wrapper);
- inStream = wrapper;
- }
- catch (Exception)
- {
- AddException(new FileNotFoundException("Failed to open the volume \"" + name + "\". Extraction is impossible."));
- inStream = null;
- return 1;
- }
- }
- return 0;
- }
- #endregion
- #region ICryptoGetTextPassword Members
-
-
-
-
-
- public int CryptoGetTextPassword(out string password)
- {
- password = Password;
- return 0;
- }
- #endregion
- #region IDisposable Members
- public void Dispose()
- {
- if (_wrappers != null)
- {
- foreach (InStreamWrapper wrap in _wrappers.Values)
- {
- wrap.Dispose();
- }
- _wrappers = null;
- }
- GC.SuppressFinalize(this);
- }
- #endregion
- }
- #endif
- }
|