Esempio n. 1
0
        public bool AddFileToImage(string wimFile, int imageIndex, string fileToAdd, string destination, IImaging.ProgressCallback progressCallback = null)
        {
            string title = $"Adding {destination} to {wimFile.Split('\\').Last()}...";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.ScanBegin:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.ScanEnd:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        break;

                    case ProgressMsg.UpdateBeginCommand:
                        break;

                    case ProgressMsg.UpdateEndCommand:
                        break;

                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.WriteMetadataEnd:
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.WriteAccess))
                {
                    wim.RegisterCallback(ProgressCallback);
                    wim.UpdateImage(
                        imageIndex,
                        UpdateCommand.SetAdd(fileToAdd, destination, null, AddFlags.None),
                        UpdateFlags.SendProgress);
                    wim.Overwrite(WriteFlags.None, Wim.DefaultThreads);
                }
            }
            catch
            {
                return(false);
            }
            return(ReseatWIMXml(wimFile));
        }
Esempio n. 2
0
        public bool CaptureImage(
            string wimFile,
            string imageName,
            string imageDescription,
            string imageFlag,
            string InputDirectory,
            string imageDisplayName                    = null,
            string imageDisplayDescription             = null,
            WimCompressionType compressionType         = WimCompressionType.Lzx,
            IImaging.ProgressCallback progressCallback = null,
            int UpdateFrom   = -1,
            bool PreserveACL = true)
        {
            string title = $"Creating {imageName} ({wimFile.Split('\\').Last()})";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.ScanBegin:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, {m.NumDirsScanned} Directories, {m.NumNonDirsScanned} Files, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.ScanDEntry:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, {m.NumDirsScanned} Directories, {m.NumNonDirsScanned} Files, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.ScanEnd:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, {m.NumDirsScanned} Directories, {m.NumNonDirsScanned} Files, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        break;

                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.WriteMetadataEnd:
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                if (File.Exists(wimFile))
                {
                    using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.WriteAccess))
                    {
                        wim.RegisterCallback(ProgressCallback);
                        wim.AddImage(InputDirectory, imageName, null, PreserveACL ? AddFlags.StrictAcls : AddFlags.NoAcls);
                        if (!string.IsNullOrEmpty(imageDescription))
                        {
                            wim.SetImageDescription((int)wim.GetWimInfo().ImageCount, imageDescription);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayName))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYNAME", imageDisplayName);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayDescription))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYDESCRIPTION", imageDisplayDescription);
                        }
                        if (!string.IsNullOrEmpty(imageFlag))
                        {
                            wim.SetImageFlags((int)wim.GetWimInfo().ImageCount, imageFlag);
                        }
                        if (UpdateFrom != -1)
                        {
                            wim.ReferenceTemplateImage((int)wim.GetWimInfo().ImageCount, UpdateFrom);
                        }
                        wim.Overwrite(WriteFlags.None, Wim.DefaultThreads);
                    }
                }
                else
                {
                    var compression = CompressionType.None;
                    switch (compressionType)
                    {
                    case WimCompressionType.Lzms:
                    {
                        compression = CompressionType.LZMS;
                        break;
                    }

                    case WimCompressionType.Lzx:
                    {
                        compression = CompressionType.LZX;
                        break;
                    }

                    case WimCompressionType.None:
                    {
                        compression = CompressionType.None;
                        break;
                    }

                    case WimCompressionType.Xpress:
                    {
                        compression = CompressionType.XPRESS;
                        break;
                    }
                    }

                    using (Wim wim = Wim.CreateNewWim(compression))
                    {
                        wim.RegisterCallback(ProgressCallback);

                        string config = @"[ExclusionList]
\$ntfs.log
\hiberfil.sys
\pagefile.sys
\swapfile.sys
\System Volume Information";

                        var configpath = Path.GetTempFileName();
                        File.Delete(configpath);
                        File.WriteAllText(configpath, config);

                        wim.AddImage(InputDirectory, imageName, configpath, PreserveACL ? AddFlags.StrictAcls : AddFlags.NoAcls);
                        if (!string.IsNullOrEmpty(imageDescription))
                        {
                            wim.SetImageDescription((int)wim.GetWimInfo().ImageCount, imageDescription);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayName))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYNAME", imageDisplayName);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayDescription))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYDESCRIPTION", imageDisplayDescription);
                        }
                        if (!string.IsNullOrEmpty(imageFlag))
                        {
                            wim.SetImageFlags((int)wim.GetWimInfo().ImageCount, imageFlag);
                        }
                        wim.Write(wimFile, Wim.AllImages, WriteFlags.None, Wim.DefaultThreads);
                        File.Delete(configpath);
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(ReseatWIMXml(wimFile));
        }
Esempio n. 3
0
        public bool ApplyImage(string wimFile, int imageIndex, string OutputDirectory, IEnumerable <string> referenceWIMs = null, bool PreserveACL = true, IImaging.ProgressCallback progressCallback = null)
        {
            string title = $"Applying {wimFile.Split('\\').Last()} - Index {imageIndex}";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.ExtractImageBegin:
                    {
                        ExtractProgress m = (ExtractProgress)info;
                    }
                    break;

                    case ProgressMsg.ExtractImageEnd:
                    {
                        ExtractProgress m = (ExtractProgress)info;
                    }
                    break;

                    case ProgressMsg.ExtractFileStructure:
                    {
                        ExtractProgress m = (ExtractProgress)info;
                        progressCallback?.Invoke($"Applying file structure ({(int)Math.Round((double)m.CurrentFileCount / m.EndFileCount * 100)}%)", 0, true);
                    }
                    break;

                    case ProgressMsg.ExtractStreams:
                    {
                        ExtractProgress m = (ExtractProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.ExtractMetadata:
                    {
                        ExtractProgress m = (ExtractProgress)info;
                        progressCallback?.Invoke($"Applying metadata ({(int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100)}%)", 0, true);
                    }
                    break;
                    }
                    return(CallbackStatus.Continue);
                }

                using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.None))
                {
                    wim.RegisterCallback(ProgressCallback);
                    if (referenceWIMs != null && referenceWIMs.Count() > 0)
                    {
                        wim.ReferenceResourceFiles(referenceWIMs, RefFlags.None, OpenFlags.None);
                    }
                    wim.ExtractImage(imageIndex, OutputDirectory, PreserveACL ? ExtractFlags.StrictAcls : ExtractFlags.NoAcls);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 4
0
        public bool ExportImage(string wimFile, string destinationWimFile, int imageIndex, IEnumerable <string> referenceWIMs = null, WimCompressionType compressionType = WimCompressionType.Lzx, IImaging.ProgressCallback progressCallback = null)
        {
            string title = $"Exporting {wimFile.Split('\\').Last()} - Index {imageIndex}";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        break;

                    case ProgressMsg.WriteMetadataEnd:
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                using (Wim srcWim = Wim.OpenWim(wimFile, OpenFlags.None))
                {
                    string imageName        = srcWim.GetImageName(imageIndex);
                    string imageDescription = srcWim.GetImageDescription(imageIndex);

                    var compression = CompressionType.None;
                    switch (compressionType)
                    {
                    case WimCompressionType.Lzms:
                    {
                        compression = CompressionType.LZMS;
                        break;
                    }

                    case WimCompressionType.Lzx:
                    {
                        compression = CompressionType.LZX;
                        break;
                    }

                    case WimCompressionType.None:
                    {
                        compression = CompressionType.None;
                        break;
                    }

                    case WimCompressionType.Xpress:
                    {
                        compression = CompressionType.XPRESS;
                        break;
                    }
                    }

                    if (referenceWIMs != null && referenceWIMs.Count() > 0)
                    {
                        srcWim.ReferenceResourceFiles(referenceWIMs, RefFlags.None, OpenFlags.None);
                    }

                    if (File.Exists(destinationWimFile))
                    {
                        using (Wim destWim = Wim.OpenWim(destinationWimFile, OpenFlags.WriteAccess))
                        {
                            destWim.RegisterCallback(ProgressCallback);

                            if (destWim.IsImageNameInUse(imageName))
                            {
                                srcWim.ExportImage(imageIndex, destWim, imageName + " " + DateTime.UtcNow.ToString(), imageDescription, ExportFlags.None);
                            }
                            else
                            {
                                srcWim.ExportImage(imageIndex, destWim, imageName, imageDescription, ExportFlags.None);
                            }

                            destWim.Overwrite(WriteFlags.None, Wim.DefaultThreads);
                        }
                    }
                    else
                    {
                        using (Wim destWim = Wim.CreateNewWim(compression))
                        {
                            destWim.RegisterCallback(ProgressCallback);
                            srcWim.ExportImage(imageIndex, destWim, imageName, imageDescription, ExportFlags.None);
                            destWim.Write(destinationWimFile, Wim.AllImages, compression == CompressionType.LZMS ? WriteFlags.Solid : WriteFlags.None, Wim.DefaultThreads);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(ReseatWIMXml(destinationWimFile));
        }
Esempio n. 5
0
        public bool ExportImage(
            string wimFile,
            string destinationWimFile,
            int imageIndex,
            IEnumerable <string> referenceWIMs         = null,
            WimCompressionType compressionType         = WimCompressionType.Lzx,
            IImaging.ProgressCallback progressCallback = null)
        {
            return(WIMLibImaging.ExportImage(wimFile,
                                             destinationWimFile,
                                             imageIndex,
                                             referenceWIMs,
                                             compressionType,
                                             progressCallback));

            string title = $"Exporting {wimFile.Split('\\').Last()} - Index {imageIndex}";

            try
            {
                WimMessageResult callback2(WimMessageType messageType, object message, object userData)
                {
                    switch (messageType)
                    {
                    case WimMessageType.Progress:
                    {
                        WimMessageProgress progressMessage = (WimMessageProgress)message;
                        progressCallback?.Invoke($"{title} (Estimated time remaining: {progressMessage.EstimatedTimeRemaining})", progressMessage.PercentComplete, false);
                        break;
                    }
                    }

                    return(WimMessageResult.Success);
                }

                using (var wimHandle = WimgApi.CreateFile(
                           wimFile,
                           WimFileAccess.Read,
                           WimCreationDisposition.OpenExisting,
                           WimCreateFileOptions.Chunked,
                           WimCompressionType.None))
                {
                    // Always set a temporary path
                    //
                    WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP"));

                    if (referenceWIMs != null)
                    {
                        foreach (var referenceFile in referenceWIMs)
                        {
                            WimgApi.SetReferenceFile(wimHandle, referenceFile, WimSetReferenceMode.Append, WimSetReferenceOptions.Chunked);
                        }
                    }

                    // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                    //
                    WimgApi.RegisterMessageCallback(wimHandle, callback2);

                    using (var newWimHandle = WimgApi.CreateFile(
                               destinationWimFile,
                               WimFileAccess.Write,
                               WimCreationDisposition.OpenAlways,
                               compressionType == WimCompressionType.Lzms ? WimCreateFileOptions.Chunked : WimCreateFileOptions.None,
                               compressionType))
                    {
                        // Always set a temporary path
                        //
                        WimgApi.SetTemporaryPath(newWimHandle, Environment.GetEnvironmentVariable("TEMP"));

                        // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                        //
                        WimgApi.RegisterMessageCallback(newWimHandle, callback2);

                        try
                        {
                            using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, imageIndex))
                            {
                                var    wiminfo      = WimgApi.GetImageInformationAsString(imageHandle);
                                var    wiminfoclass = WIMInformationXML.DeserializeIMAGE(wiminfo);
                                string imagename    = wiminfoclass.DISPLAYNAME == null ? wiminfoclass.NAME : wiminfoclass.DISPLAYNAME;

                                title = $"Exporting {imagename} ({wimFile.Split('\\').Last()} - Index {imageIndex})";

                                // Export the image contents
                                // This call is blocking but WIMGAPI will be calling MyCallbackMethod() during the process
                                //
                                WimgApi.ExportImage(imageHandle, newWimHandle, WimExportImageOptions.AllowDuplicates);
                            }
                        }
                        finally
                        {
                            // Be sure to unregister the callback method
                            //
                            WimgApi.UnregisterMessageCallback(wimHandle, callback2);

                            // Be sure to unregister the callback method
                            //
                            WimgApi.UnregisterMessageCallback(newWimHandle, callback2);
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Esempio n. 6
0
        public bool CaptureImage(
            string wimFile,
            string imageName,
            string imageDescription,
            string imageFlag,
            string InputDirectory,
            string imageDisplayName                    = null,
            string imageDisplayDescription             = null,
            WIMInformationXML.WINDOWS windows          = null,
            WimCompressionType compressionType         = WimCompressionType.Lzx,
            WimCaptureImageOptions addFlags            = WimCaptureImageOptions.None,
            IImaging.ProgressCallback progressCallback = null)
        {
            string title = $"Creating {imageName} ({wimFile.Split('\\').Last()})";

            try
            {
                int directoriesScanned = 0;
                int filesScanned       = 0;

                WimMessageResult callback2(WimMessageType messageType, object message, object userData)
                {
                    switch (messageType)
                    {
                    case WimMessageType.Process:
                    {
                        WimMessageProcess processMessage = (WimMessageProcess)message;
                        if (processMessage.Path.StartsWith(Path.Combine(InputDirectory.EndsWith(":") ? InputDirectory + @"\" : InputDirectory, @"System Volume Information"), StringComparison.InvariantCultureIgnoreCase))
                        {
                            processMessage.Process = false;
                        }
                        break;
                    }

                    case WimMessageType.Progress:
                    {
                        WimMessageProgress progressMessage = (WimMessageProgress)message;
                        progressCallback?.Invoke($"{title} (Estimated time remaining: {progressMessage.EstimatedTimeRemaining})", progressMessage.PercentComplete, false);
                        break;
                    }

                    case WimMessageType.Scanning:
                    {
                        WimMessageScanning scanningMessage = (WimMessageScanning)message;

                        switch (scanningMessage.CountType)
                        {
                        case WimMessageScanningType.Directories:
                        {
                            directoriesScanned = scanningMessage.Count;
                            break;
                        }

                        case WimMessageScanningType.Files:
                        {
                            filesScanned = scanningMessage.Count;
                            break;
                        }
                        }

                        progressCallback?.Invoke($"Scanning objects ({filesScanned} files, {directoriesScanned} directories scanned)", 0, true);
                        break;
                    }
                    }

                    return(WimMessageResult.Success);
                }

                using (var wimHandle = WimgApi.CreateFile(
                           wimFile,
                           WimFileAccess.Write,
                           WimCreationDisposition.OpenAlways,
                           compressionType == WimCompressionType.Lzms ? WimCreateFileOptions.Chunked : WimCreateFileOptions.None,
                           compressionType))
                {
                    // Always set a temporary path
                    //
                    WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP"));

                    // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                    //
                    WimgApi.RegisterMessageCallback(wimHandle, callback2);

                    try
                    {
                        using (var imagehandle = WimgApi.CaptureImage(wimHandle, InputDirectory, addFlags))
                        {
                            var wiminfo      = WimgApi.GetImageInformationAsString(imagehandle);
                            var wiminfoclass = WIMInformationXML.DeserializeIMAGE(wiminfo);
                            if (!string.IsNullOrEmpty(imageFlag))
                            {
                                wiminfoclass.FLAGS = imageFlag;
                            }
                            if (!string.IsNullOrEmpty(imageName))
                            {
                                wiminfoclass.NAME = imageName;
                            }
                            if (!string.IsNullOrEmpty(imageDescription))
                            {
                                wiminfoclass.DESCRIPTION = imageDescription;
                            }
                            if (!string.IsNullOrEmpty(imageDisplayName))
                            {
                                wiminfoclass.DISPLAYNAME = imageDisplayName;
                            }
                            if (!string.IsNullOrEmpty(imageDisplayDescription))
                            {
                                wiminfoclass.DISPLAYDESCRIPTION = imageDisplayDescription;
                            }
                            if (windows != null)
                            {
                                wiminfoclass.WINDOWS = windows;
                            }

                            wiminfo = WIMInformationXML.SerializeIMAGE(wiminfoclass);
                            WimgApi.SetImageInformation(imagehandle, wiminfo);
                        }
                    }
                    finally
                    {
                        // Be sure to unregister the callback method
                        //
                        WimgApi.UnregisterMessageCallback(wimHandle, callback2);
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }