winimageiso.cs

来自「对ima、imz压缩文件修改」· CS 代码 · 共 727 行 · 第 1/3 页

CS
727
字号
                {
                    if (cPath == String.Empty) { return Item; } else { return Item.Children.LocateValue(cPath); }
                }
            }
            return null;
        }
        public new void Remove(string Key)
        {
            string[] IDs = Key.Split(':');
            string lKey = (Parent != null ? Parent.Key + ":" : "") + IDs[0];
            if (base.ContainsKey(lKey))
            {
                string cKey = "";
                for (int i = 1; i <= IDs.GetUpperBound(0); i++) { cKey += IDs[i] + (i < IDs.GetUpperBound(0) ? ":" : ""); }
                if (cKey.Length == 0) { base.Remove(lKey); } else { base[lKey].Children.Remove(cKey); }
            }
        }
        public new IsoObject this[string Key]
        {
            get
            {
                string[] IDs = Key.Split(':');
                string lKey = (Parent != null ? Parent.Key + ":" : "") + IDs[0];
                if (base.ContainsKey(lKey))
                {
                    string cKey = "";
                    for (int i = 1; i <= IDs.GetUpperBound(0); i++) { cKey += IDs[i] + (i < IDs.GetUpperBound(0) ? ":" : ""); }
                    if (cKey.Length == 0) { return base[lKey]; } else { return base[lKey].Children[cKey]; }
                }
                else { return null; }
            }
        }
    }

    internal class BulkWorker
    {
        #region Declarations
            private string Action = "";
            private Wimadll.WimCB CallBack;
            public string Destination = "";
            private int iCBResult = (int)WinImage.CallBackResult.OK;
            private IntPtr iImgHandle = IntPtr.Zero;
            private int iProcCount = 0;
            public ProgressDialog.IProgressCallback ProgDisplay = null;
            public IsoObject Root = null;
            public bool Success = false;
            public WinImageError LastError = new WinImageError();
            private const int DWEV_NEXTFILE = 0x7FFF0010;
        #endregion
        public BulkWorker(IntPtr Handle)
        {
            iImgHandle = Handle;
            CallBack = new Wimadll.WimCB(Progress);
        }
        public void ExtractFiles()
        {
            if (ProgDisplay != null)
            {
                ProgDisplay.Begin(true);
                ProgDisplay.SetRange_Primary(0, Root.SelectedFiles + Root.SelectedFolders);
                ProgDisplay.SetRange_Secondary(0, 100);
                ProgDisplay.SetCaption("Extracting Files");
            }
            Action = "Extracting: ";
            try
            {
                Success = ExtFiles(Destination, Root);
            }
            catch (InvalidDataException e)
            {
                LastError.ErrorCause = e.Message;
                LastError.ErrorSource = "ExtractFiles";
                LastError.ErrorType = WinImageError.EType.InvalidData;
                Success= false;
            }
            catch (InvalidOperationException e)
            {
                LastError.ErrorCause = e.Message;
                LastError.ErrorSource = "ExtractFiles";
                LastError.ErrorType = WinImageError.EType.InvalidOperation;
                Success = false;
            }
            catch (Exception e)
            {
                LastError.ErrorCause = e.Message;
                LastError.ErrorSource = "ExtractFiles";
                LastError.ErrorType = WinImageError.EType.Other;
                Success = false;
            }
            if (ProgDisplay != null) { ProgDisplay.End(); }
        }
        private bool ExtFiles(string sDestPath, IsoObject isoCurParent)
        {
            bool Success = true;
            foreach (IsoObject item in isoCurParent.Children)
            {
                if (iCBResult == (int)WinImage.CallBackResult.Cancel) { Success = false; break; }
                if (item.Selected || item.SelectedChildren)
                {
                    if (item.ObjectType == IsoObject.ObjType.otDir)
                    {
                        string sOutputDir = Path.Combine(sDestPath, item.LongName);
                        if (!Directory.Exists(sOutputDir)) { Directory.CreateDirectory(sOutputDir); }
                        if (item.Selected) { iCBResult = CallBack(DWEV_NEXTFILE, (uint)++iProcCount, 0, 0, 0); }
                        if (iCBResult == (int)WinImage.CallBackResult.Cancel) { Success = false; break; }
                        if (item.SelectedChildren)
                        {
                            Wimadll.ChszDir(iImgHandle, item.ShortName);
                            Success = Success && ExtFiles(sOutputDir, item);
                            Wimadll.ChDir(iImgHandle, Wimadll.CDM_UPPER);
                        }
                    }
                    else
                    {
                        string sOutputFile = Path.Combine(sDestPath, item.LongName);
                        StringBuilder OFile = new StringBuilder(260);
                        if (File.Exists(sOutputFile)){File.Delete(sOutputFile);}
                        Success = Success && Wimadll.ExtractFileCB(iImgHandle, CallBack, IntPtr.Zero, item.Position, sDestPath, OFile);
                        iCBResult = CallBack(DWEV_NEXTFILE, (uint)++iProcCount, 0, 0, 0);
                        if (iCBResult == (int)WinImage.CallBackResult.Cancel) { Success = false; break; }
                    }
                }
            }
            if (iCBResult == (int)WinImage.CallBackResult.Cancel) { throw new Exception("Operation Aborted By The User."); }
            return Success;
        }
        private int Progress(uint dwEvent, uint dwEventParam, uint dwWin32Err, int lpParam, int lpUserParam)
        {
            switch (dwEvent)
            {
                case DWEV_NEXTFILE:
                    if (ProgDisplay != null) { ProgDisplay.Increment_Primary(1); }
                    break;
                case Wimadll.DWEV_PROGRESSPERCENT:
                    if (ProgDisplay != null)
                    {
                        if (ProgDisplay.Stages == 2) { ProgDisplay.StepTo_Secondary((int)dwEventParam); } else { ProgDisplay.StepTo_Primary((int)dwEventParam); }
                        Wimadll.PROGRESSFILE_SUPINFO psInfo = (Wimadll.PROGRESSFILE_SUPINFO)Marshal.PtrToStructure((IntPtr)lpParam, typeof(Wimadll.PROGRESSFILE_SUPINFO));
                        if (psInfo.dwCurrentPos == 0) { ProgDisplay.SetText(Action + psInfo.lpszFullName + "\nTo: " + psInfo.lpszName); }
                    }
                    break;
                default:
                    Console.WriteLine("Callback Event: " + dwEvent.ToString());
                    break;
            }
            if (ProgDisplay == null) { return (int)WinImage.CallBackResult.OK; } else { return ProgDisplay.IsAborting ? (int)WinImage.CallBackResult.Cancel : (int)WinImage.CallBackResult.OK; }
        }
    }
    /// <summary>
    /// This class is used to return information about
    /// the last error that was encountered by the WinImage
    /// Library.
    /// </summary>
    public class WinImageError
    {
        /// <summary>
        /// This type defines the kinds of errors that
        /// can be returned by the PDTB Library.
        /// </summary>
        public enum EType
        {
            /// <summary>This means that no error has occured.</summary>
            None = 0,
            /// <summary>This means that some of the data entered by the calling application is either incomplete or invalid.</summary>
            InvalidData = 1,
            /// <summary>This means that an operation was performed illegally.  Perhaps an attempt to encrypt was made before the Book Key was set, or to Randomize Names with a Key that doesn't support that security level.</summary>
            InvalidOperation = 2,
            /// <summary>This means that an unexpected exception was encountered.</summary>
            Other = 3
        }
        private EType ErrType;
        private string ErrCause;
        private string ErrSource;
        /// <summary>
        /// This Read-Only property allows applications to retrieve
        /// information about the cause of the last error.
        /// </summary>
        /// <value>The cause of the last error.</value>
        public string ErrorCause
        {
            get { return ErrCause; }
            internal set { ErrCause = value; }
        }
        /// <summary>
        /// This Read-Only property allows applications to retrieve
        /// information about the source of the last error.
        /// </summary>
        /// <value>The source of the last error.</value>
        public string ErrorSource
        {
            get { return ErrSource; }
            internal set { ErrSource = value; }
        }
        /// <summary>
        /// This Read-Only property allows applications to retrieve
        /// information about the type of the last error.
        /// </summary>
        /// <remarks>
        /// See the description of the <see cref="EType"/> enumerated
        /// type for more information about the possible error types.
        /// </remarks>
        /// <value>The type of the last error.</value>
        public EType ErrorType
        {
            get { return ErrType; }
            internal set { ErrType = value; }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PDTBError"/> class.
        /// </summary>
        internal WinImageError()
        {
            //Making this constructor an internal method allows outside
            //applications to see this class, since it is a public class,
            //but they can not CREATE an instance of this class, only 
            //reference instances created by the library.
            ErrCause = "";
            ErrSource = "";
            ErrType = EType.None;
        }
    }

    internal static class WinImageHelpers
    {
        [DllImport("KERNEL32.DLL", EntryPoint = "GetDiskFreeSpaceExW", CharSet = CharSet.Unicode)]
        private static extern bool GetDiskFreeSpace(string drive, out long FreeUserBytes, out long TotalBytes, out long TotalFreeBytes);
        internal static long GetFreeSpace(string sDestination)
        {
            long lUserBytes = 0;
            long lTotalBytes = 0;
            long lFreeBytes = 0;
            bool bRet = GetDiskFreeSpace(Path.GetPathRoot(sDestination), out lUserBytes, out lTotalBytes, out lFreeBytes);
            //If we get a free space value back from the GetDiskFreeSpace function, then
            //act accordingly, otherwise, ALWAYS return 0.
            if (bRet) { return lUserBytes; } else { return 0; }
        }
    }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?