internal void AddFile(string name, Stream stream, FileAttributes attributes, DateTime lastWriteTime, bool execute, CabinetCompressionLevel compLevel) { this.fileStream = stream; this.fileAttributes = attributes & (FileAttributes.Archive | FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System); this.fileLastWriteTime = lastWriteTime; this.status.currentFileName = name; try { this.erf.Clear(); FCI.TCOMP tcomp = GetCompressionType(compLevel); if (Encoding.UTF8.GetByteCount(name) > name.Length) { byte[] nameBytes = Encoding.UTF8.GetBytes(name); name = Encoding.Default.GetString(nameBytes); this.fileAttributes |= FileAttributes.Normal; // _A_NAME_IS_UTF } FCI.AddFile(this.fciHandle, CabBase.STREAM_FILE, name, execute, this.fciGetNextCabinetHandler, this.fciStatusHandler, this.fciGetOpenInfoHandler, tcomp); if (this.erf.fError) { throw new CabinetCreateException(this.erf.erfOper, this.erf.erfType, this.abortException); } } finally { this.fileStream = null; this.status.currentFileName = null; } }
/// <summary> /// Creates a cabinet and writes it to a stream. /// </summary> /// <param name="stream">The stream for writing the cabinet.</param> /// <param name="files">The names of the files in the cabinet (not external file paths).</param> /// <param name="compLevel">The cabinet compression level.</param> /// <param name="openFileHandler">Callback for opening streams for reading included files.</param> /// <param name="closeFileHandler">Callback for closing included file streams. This may be null, in which /// case the stream's <see cref="Stream.Close"/> method will be called.</param> /// <param name="openFileContext">User context object that will be passed to the /// <paramref name="openFileHandler"/> and <paramref name="closeFileHandler"/>.</param> /// <param name="statusCallback">Callback for reporting creation progress. This may be null /// if status is not desired.</param> /// <param name="statusContext">User context object passed to the <paramref name="statusCallback"/>.</param> /// <exception cref="CabinetCreateException">The cabinet could not be created.</exception> public static void Create(Stream stream, string[] files, CabinetCompressionLevel compLevel, CabinetCreateOpenFileHandler openFileHandler, CabinetCreateCloseFileHandler closeFileHandler, object openFileContext, CabinetStatusCallback statusCallback, object statusContext) #endif // !CABMINIMAL { if (stream == null) { throw new ArgumentNullException("stream"); } Cabinet.Create(null, null, 0, 0, new CabinetOpenCabHandler(Cabinet.DefaultOpenCabHandler), new CabinetCloseCabHandler(Cabinet.DefaultCloseCabHandler), stream, new string[][] { files }, compLevel, openFileHandler, closeFileHandler, openFileContext #if !CABMINIMAL , statusCallback, statusContext #endif // !CABMINIMAL ); }
internal static FCI.TCOMP GetCompressionType(CabinetCompressionLevel compLevel) { if (compLevel == CabinetCompressionLevel.None) { return(FCI.TCOMP.TYPE_NONE); } else { if (compLevel < CabinetCompressionLevel.None) { compLevel = CabinetCompressionLevel.None; } if (compLevel > CabinetCompressionLevel.Max) { compLevel = CabinetCompressionLevel.Max; } return((FCI.TCOMP)((int)FCI.TCOMP.TYPE_LZX | ((int)FCI.TCOMP.LZX_WINDOW_LO + ((compLevel - CabinetCompressionLevel.Min) << (int)FCI.TCOMP.SHIFT_LZX_WINDOW)))); } }
/// <summary> /// Creates a cabinet. /// </summary> /// <param name="cabPath">Path of cabinet to create.</param> /// <param name="compressionLevel">Level of compression to apply.</param> /// <param name="maxFiles">Maximum number of files that will be added to cabinet.</param> /// <param name="maxSize">Maximum size of cabinet.</param> /// <param name="maxThresh">Maximum threshold for each cabinet.</param> public void Compress(IEnumerable <CabinetCompressFile> files, CabinetCompressionLevel compressionLevel, int maxSize = 0, int maxThresh = 0) { var compressionLevelVariable = Environment.GetEnvironmentVariable(CompressionLevelVariable); // Override authored compression level if environment variable is present. if (!String.IsNullOrEmpty(compressionLevelVariable)) { if (!Enum.TryParse(compressionLevelVariable, true, out compressionLevel)) { //throw new WixException(WixErrors.IllegalEnvironmentVariable(CompressionLevelVariable, compressionLevelVariable)); throw new ArgumentException(); } } var wixnative = new WixNativeExe("smartcab", this.Path, Convert.ToInt32(compressionLevel), files.Count(), maxSize, maxThresh); foreach (var file in files) { wixnative.AddStdinLine(file.ToWixNativeStdinLine()); } wixnative.Run();
/// <summary> /// Compresses files into the cabinet file, specifying the names used to store the files in the cabinet. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if specified it is the root directory /// for any relative paths in <paramref name="filenameMap"/>.</param> /// <param name="filenameMap">A mapping from internal file paths to external file paths.</param> /// <param name="compLevel">The compression level used when creating the cabinet.</param> /// <param name="callback">Handler for receiving progress information; this may be null if progress is not desired.</param> /// <param name="context">User context object passed to the <paramref name="callback"/>, may be null.</param> internal virtual void CompressFileSet(string sourceDirectory, IDictionary filenameMap, CabinetCompressionLevel compLevel, CabinetStatusCallback callback, object context) { if (filenameMap == null) { throw new ArgumentNullException("filenameMap"); } string[] fileNames = new string[filenameMap.Count]; filenameMap.Keys.CopyTo(fileNames, 0); using (Stream stream = this.GetCabinetWriteStream()) { Cabinet.Create(stream, fileNames, compLevel, new CabinetCreateOpenFileHandler(CabinetInfo.CreateOpenFileHandler), new CabinetCreateCloseFileHandler(CabinetInfo.CreateCloseFileHandler), new object[] { sourceDirectory, filenameMap }, callback, context); } }
/// <summary> /// Compresses files into the cabinet file, specifying the names used to store the files in the cabinet. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if specified it is the root directory /// for any relative paths in <paramref name="sourceFileNames"/>.</param> /// <param name="sourceFileNames">The list of files to be included in the cabinet.</param> /// <param name="fileNames">The names of the files as they are stored in the cabinet. Each name /// includes the internal path of the file, if any. This parameter may be null, in which case the /// files are stored in the cabinet with their source file names and no path information.</param> /// <param name="compLevel">The compression level used when creating the cabinet.</param> /// <param name="callback">Handler for receiving progress information; this may be null if progress is not desired.</param> /// <param name="context">User context object passed to the <paramref name="callback"/>, may be null.</param> /// <remarks> /// Duplicate items in the <paramref name="fileNames"/> array will cause a <see cref="CabinetCreateException"/>. /// </remarks> internal virtual void CompressFiles(string sourceDirectory, string[] sourceFileNames, string[] fileNames, CabinetCompressionLevel compLevel, CabinetStatusCallback callback, object context) { if (sourceFileNames == null) { if (sourceDirectory == null) { throw new ArgumentNullException("sourceDirectory", "Either sourceDirectory or sourceFileNames must be non-null."); } sourceFileNames = System.IO.Directory.GetFiles(sourceDirectory); } if (fileNames == null) { fileNames = new string[sourceFileNames.Length]; for (int i = 0; i < sourceFileNames.Length; i++) { fileNames[i] = Path.GetFileName(sourceFileNames[i]); } } using (Stream stream = this.GetCabinetWriteStream()) { Cabinet.Create(stream, fileNames, compLevel, new CabinetCreateOpenFileHandler(CabinetInfo.CreateOpenFileHandler), new CabinetCreateCloseFileHandler(CabinetInfo.CreateCloseFileHandler), new object[] { sourceDirectory, CreateStringDictionary(fileNames, sourceFileNames) }, callback, context); } }
/// <summary> /// Compresses all files in a directory into the cabinet file, optionally including subdirectories. /// The files are stored in the cabinet using their relative file paths in the directory tree. /// Note, while this library fully supports an internal directory structure in cabinets, /// some extraction tools do not. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if specified it is the root directory /// for any relative paths in source file.</param> /// <param name="includeSubdirectories">If true, recursively include files in subdirectories.</param> /// <param name="compLevel">The compression level used when creating the cabinet.</param> /// <param name="callback">Handler for receiving progress information; this may be null if progress is not desired.</param> /// <param name="context">User context object passed to the callback, may be null.</param> internal void CompressDirectory(string sourceDirectory, bool includeSubdirectories, CabinetCompressionLevel compLevel, CabinetStatusCallback callback, object context) { string[] files = GetRelativeFilePathsInDirectoryTree(sourceDirectory, includeSubdirectories); this.CompressFiles(sourceDirectory, files, files, compLevel, callback, context); }
public static void Create(CabinetNameHandler nameHandler, object nameContext, long maxCabSize, long maxFolderSize, CabinetOpenCabHandler openCabHandler, CabinetCloseCabHandler closeCabHandler, object openCabContext, string[][] foldersAndFiles, CabinetCompressionLevel compLevel, CabinetCreateOpenFileHandler openFileHandler, CabinetCreateCloseFileHandler closeFileHandler, object openFileContext, CabinetStatusCallback statusCallback, object statusContext) #endif // !CABMINIMAL { using (CabCreator cabInstance = new CabCreator(maxCabSize, maxFolderSize)) { cabInstance.nameHandler = nameHandler; cabInstance.nameContext = nameContext; cabInstance.openCabHandler = openCabHandler; cabInstance.closeCabHandler = closeCabHandler; cabInstance.openCabContext = openCabContext; cabInstance.openFileHandler = openFileHandler; cabInstance.closeFileHandler = closeFileHandler; cabInstance.openFileContext = openFileContext; #if !CABMINIMAL cabInstance.statusCallback = statusCallback; cabInstance.statusContext = statusContext; if (cabInstance.statusCallback != null) { cabInstance.status.totalFolders = (short)foldersAndFiles.Length; for (int iFolder = 0; iFolder < foldersAndFiles.Length; iFolder++) { string[] files = foldersAndFiles[iFolder]; for (int iFile = 0; iFile < files.Length; iFile++) { FileAttributes attributes; DateTime lastWriteTime; Stream fileStream = openFileHandler(files[iFile], out attributes, out lastWriteTime, openFileContext); if (fileStream != null) { cabInstance.status.totalFileBytes += fileStream.Length; cabInstance.status.totalFiles++; } closeFileHandler(files[iFile], fileStream, openFileContext); } } } #endif // !CABMINIMAL for (int iFolder = 0; iFolder < foldersAndFiles.Length; iFolder++) { string[] files = foldersAndFiles[iFolder]; for (int iFile = 0; iFile < files.Length; iFile++) { FileAttributes attributes; DateTime lastWriteTime; Stream fileStream = openFileHandler(files[iFile], out attributes, out lastWriteTime, openFileContext); if (fileStream != null) { #if !CABMINIMAL if (cabInstance.statusCallback != null) { if (cabInstance.status.currentFolderTotalBytes > 0) { cabInstance.status.currentFolderBytesProcessed = cabInstance.status.currentFolderTotalBytes; cabInstance.status.statusType = CabinetStatusType.FinishFolder; cabInstance.statusCallback(cabInstance.status, cabInstance.statusContext); cabInstance.status.currentFolderBytesProcessed = cabInstance.status.currentFolderTotalBytes = 0; if (!(iFolder == 0 && iFile == 0)) { cabInstance.status.currentFolderNumber++; if (cabInstance.status.totalFolders <= cabInstance.status.currentFolderNumber) { cabInstance.status.totalFolders = (short)(cabInstance.status.currentFolderNumber + 1); } } } } #endif // !CABMINIMAL cabInstance.status.currentFileName = files[iFile]; if (!(iFolder == 0 && iFile == 0)) { cabInstance.status.currentFileNumber++; } #if !CABMINIMAL if (cabInstance.statusCallback != null) { cabInstance.status.currentFileTotalBytes = fileStream.Length; cabInstance.status.currentFileBytesProcessed = 0; cabInstance.status.statusType = CabinetStatusType.StartFile; cabInstance.statusCallback(cabInstance.status, cabInstance.statusContext); } #endif // !CABMINIMAL cabInstance.AddFile(files[iFile], fileStream, attributes, lastWriteTime, false, compLevel); } } cabInstance.FlushFolder(); } cabInstance.FlushCabinet(); } }
public static void Create(CabinetNameHandler nameHandler, object nameContext, long maxCabSize, long maxFolderSize, CabinetOpenCabHandler openCabHandler, CabinetCloseCabHandler closeCabHandler, object openCabContext, string[][] foldersAndFiles, CabinetCompressionLevel compLevel, CabinetCreateOpenFileHandler openFileHandler, CabinetCreateCloseFileHandler closeFileHandler, object openFileContext)
/// <summary> /// Creates a cabinet and writes it to a stream. /// </summary> /// <param name="stream">The stream for writing the cabinet.</param> /// <param name="files">The names of the files in the cabinet (not external file paths).</param> /// <param name="compLevel">The cabinet compression level.</param> /// <param name="openFileHandler">Callback for opening streams for reading included files.</param> /// <param name="closeFileHandler">Callback for closing included file streams. This may be null, in which /// case the stream's <see cref="Stream.Close"/> method will be called.</param> /// <param name="openFileContext">User context object that will be passed to the /// <paramref name="openFileHandler"/> and <paramref name="closeFileHandler"/>.</param> /// <exception cref="CabinetCreateException">The cabinet could not be created.</exception> public static void Create(Stream stream, string[] files, CabinetCompressionLevel compLevel, CabinetCreateOpenFileHandler openFileHandler, CabinetCreateCloseFileHandler closeFileHandler, object openFileContext)