winimageiso.cs

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

CS
727
字号
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using WinImageSample;

namespace WinImage
{
    public enum OverwriteType { YES = 0, NO = 1, PROMPT = 2 };
    public enum CallBackResult { OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, No = 7 };
    public class ISOImage : IDisposable
    {
        #region Variable Declarations
            private WinImageError wiError = new WinImageError();
            private IsoObjectCollection FileCol = new IsoObjectCollection(null);
            private int iFileID = 0;
            private int iFolderID = 0;
            private IntPtr iImgHandle = (IntPtr)(-1);
            public OverwriteType Overwrite = OverwriteType.YES;
            private IsoObject Root = null;
            private string sFileName = "";
            private string sFilePath = "";
            public bool Silent = false;
        #endregion
        #region Properties
            public IsoObjectCollection Files { get { return FileCol; } }
            public string ImageFileName { get { return sFileName; } }
            public string ImageFilePath { get { return sFilePath; } }
            public IsoObject ImageRoot { get { return Root; } }
            public string Label
            {
                get
                {
                    if (iImgHandle != IntPtr.Zero)
                    {
                        StringBuilder ImageLabel = new StringBuilder(' ', 255);
                        Wimadll.GetLabel(iImgHandle, ImageLabel);
                        return ImageLabel.ToString().Trim();
                    }
                    else { return ""; }
                }
                set { Wimadll.SetLabel(iImgHandle, value); }
            }
            public WinImageError LastError { get { return wiError; } }
            public int SelectedFiles { get { return Root.SelectedFiles; } }
            public int SelectedFolders { get { return Root.SelectedFolders; } }
            public long SelectedSize { get { return Root.SelectedSize; } }
            public int TotalFiles { get { return iFileID; } }
            public int TotalFolders { get { return iFolderID; } }
            public long TotalSize { get { return Root.Size; } }
        #endregion
        #region Public Methods
            public void DisplayImage(System.Windows.Forms.TreeView DisplayTree)
            {
                DisplayTree.Nodes.Clear();
                ThreeStateTreeNode dRoot = new ThreeStateTreeNode(this.ImageFileName);
                dRoot.Name = "root";
                DisplayTree.Nodes.Add(dRoot);
                DisplayImageNodes(dRoot, this.ImageRoot);
                dRoot.Expand();
            }
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            public bool ExtractFiles() { return ExtractFiles(sFilePath); }
            public bool ExtractFiles(string Destination)
            {
                if (iImgHandle == IntPtr.Zero)
                {
                    wiError.ErrorCause = "No Image Loaded For File Extraction.";
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = WinImageError.EType.InvalidOperation;
                    return false;
                }
                if (SelectedFiles == 0 && SelectedFolders == 0)
                {
                    wiError.ErrorCause = "No Files Or Folders Selected For Extraction.";
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = WinImageError.EType.InvalidOperation;
                    return false;
                }
                if (!Path.IsPathRooted(Destination)) { Destination = Path.Combine(sFilePath, Destination); }
                if (Root.SelectedSize > WinImageHelpers.GetFreeSpace(Destination))
                {
                    wiError.ErrorCause = "Insufficient Space For Extracted Files.";
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = WinImageError.EType.InvalidOperation;
                    return false;
                }
                if (!Directory.Exists(Destination)) { Directory.CreateDirectory(Destination); }
                BulkWorker objWork = new BulkWorker(iImgHandle);
                objWork.Destination = Destination;
                if (CheckExistance(Destination, Root))
                {
                    //Exisitng File(s), so decide what to do
                    switch (Overwrite)
                    {
                        case OverwriteType.PROMPT:
                            if (Silent)
                            {
                                wiError.ErrorCause = "Prompting For Overwrite Is Not Allowed When The 'Silent' Property Is Set To True.";
                                wiError.ErrorSource = "ExtractFiles";
                                wiError.ErrorType = WinImageError.EType.InvalidOperation;
                                return false;
                            }
                            string sPrompt = "One Or More Of The Files Selected For Extraction Already Exist In The Destination Directory.\nAre You Sure You Wish To Overwrite These Files?";
                            if (System.Windows.Forms.MessageBox.Show(sPrompt, "Overwrite Files", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                            {
                                //IF they don't want to overwrite then kill the display and exit with an error for information back to the calling procedure
                                wiError.ErrorCause = "One Or More Files Already Exist In The Destination Directory.";
                                wiError.ErrorSource = "ExtractFiles";
                                wiError.ErrorType = WinImageError.EType.InvalidOperation;
                                return false;
                            }
                            //Otherwise, temporarily override the user's OverWrite value and tell child procedures to always overwrite (since that is what the user requested if we got here).
                            break;
                        case OverwriteType.YES:
                            break;
                        default:
                            wiError.ErrorCause = "One Or More Files Already Exist In The Destination Directory.";
                            wiError.ErrorSource = "ExtractFiles";
                            wiError.ErrorType = WinImageError.EType.InvalidOperation;
                            return false;
                    }
                }
                objWork.Root = Root;
                try
                {
                    if (!Silent)
                    {
                        ProgressDialog.TwoStageProgressWindow pDisplay = new ProgressDialog.TwoStageProgressWindow();
                        objWork.ProgDisplay = (pDisplay as ProgressDialog.IProgressCallback);
                        Thread Work = new Thread(new ThreadStart(objWork.ExtractFiles));
                        Work.Start();
                        pDisplay.ShowDialog();
                        pDisplay = null;
                        Work = null;
                    }
                    else { objWork.ExtractFiles(); }
                }
                catch (InvalidDataException e)
                {
                    wiError.ErrorCause = e.Message;
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = WinImageError.EType.InvalidData;
                    return false;
                }
                catch (InvalidOperationException e)
                {
                    wiError.ErrorCause = e.Message;
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = WinImageError.EType.InvalidOperation;
                    return false;
                }
                catch (Exception e)
                {
                    wiError.ErrorCause = e.Message;
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = WinImageError.EType.Other;
                    return false;
                }
                bool bResult = objWork.Success;
                if (!bResult)
                {
                    wiError.ErrorCause = objWork.LastError.ErrorCause;
                    wiError.ErrorSource = "ExtractFiles";
                    wiError.ErrorType = objWork.LastError.ErrorType;
                }
                objWork = null;
                return bResult;
            }
            ~ISOImage() { Dispose(); }
            public ISOImage(string ImageName)
            {
                if (!File.Exists(ImageName))
                {
                    wiError.ErrorCause = "Specified Image Does Not Exist Or Is Not Accessible.\n" + ImageName;
                    wiError.ErrorSource = "Load ISOImage";
                    wiError.ErrorType = WinImageError.EType.InvalidData;
                    return;
                }
                sFileName = System.IO.Path.GetFileName(ImageName);
                sFilePath = System.IO.Path.GetDirectoryName(ImageName);
                iImgHandle = Wimadll.CreateCDIsoIma(ImageName);
                StringBuilder rootDir = new StringBuilder(' ', Wimadll.MAXLFN);
                if (Wimadll.GetCurDir(iImgHandle, rootDir, (uint)Wimadll.MAXLFN))
                {
                    IsoObject root = new IsoObject();
                    root.ShortName = rootDir.ToString();
                    root.LongName = rootDir.ToString();
                    root.ObjectType = IsoObject.ObjType.otDir;
                    root.Key = "root";
                    FileCol.Add(root.Key, root);
                    Root = FileCol["root"];
                    ReadFileList(Root, "root");
                }
            }
        #endregion
        #region Private Methods
            private bool CheckExistance(string DestinationPath, IsoObject isoCurRoot)
            {
                bool bExist = false;
                foreach (IsoObject Item in isoCurRoot.Children)
                {
                    string strOutput = Path.Combine(DestinationPath,Item.LongName);
                    if (Item.ObjectType == IsoObject.ObjType.otDir) { bExist = bExist || CheckExistance(strOutput, Item); } else { bExist = bExist || File.Exists(strOutput); }
                    if (bExist) { break; }
                }
                return bExist;
            }
            private void DisplayImageNodes(ThreeStateTreeNode DisplayRoot, IsoObject ImageRoot)
            {
                foreach (IsoObject ImageChild in ImageRoot.Children)
                {
                    ThreeStateTreeNode curNode = new ThreeStateTreeNode(ImageChild.LongName);
                    curNode.Name = ImageChild.Key;
                    DisplayRoot.Nodes.Add(curNode);
                    if (ImageChild.ObjectType == IsoObject.ObjType.otDir) { DisplayImageNodes(curNode, ImageChild); }
                }
            }
            protected virtual void Dispose(bool disposing)
            {
                if (iImgHandle != IntPtr.Zero)
                {
                    Wimadll.DeleteIma(iImgHandle);
                    iImgHandle = (IntPtr)(-1);
                }
                if (FileCol.Count > 0) { FileCol.Clear(); }
                iFileID = 0;
                iFolderID = 0;
                Root = null;
            }
            private void ReadFileList(IsoObject isoCurRoot, string sParentID)
            {
                long lFiles = Wimadll.GetNbEntryCurDir(iImgHandle);
                Wimadll.DIRINFO[] lpDi = new Wimadll.DIRINFO[lFiles];
                if (Wimadll.GetDirInfo(iImgHandle, lpDi, Wimadll.SORT_NAME))
                {
                    for (int i = 0; i < lFiles; i++)

⌨️ 快捷键说明

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