/// <summary> /// Determines whether a file, identified by a file name, is a cabinet file. /// </summary> /// <param name="filename">The name of the file to be tested.</param> /// <param name="cabinfo">A <see cref="FdiCabinetInfo"/> object that will receive information about the cabinet file.</param> /// <returns>Returns True if the file is a cabinet. If so, the cabinfo object is filled with information about the cabinet file. /// Returns False if the file is not a cabinet.</returns> public bool IsCabinetFile(string filename, FdiCabinetInfo cabinfo) { if (disposed) { throw new ObjectDisposedException("CabDecompressor"); } using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { return(IsCabinetFile(fs, cabinfo)); } }
/// <summary> /// Determines whether a file, identified by a stream, is a cabinet file. /// </summary> /// <param name="fstream">The open file stream to be tested.</param> /// <param name="cabinfo">A <see cref="FdiCabinetInfo"/> object that will receive information about the cabinet file.</param> /// <returns>Returns True if the file is a cabinet. If so, the cabinfo object is filled with information about the cabinet file. /// Returns False if the file is not a cabinet.</returns> public bool IsCabinetFile(Stream fstream, FdiCabinetInfo cabinfo) { if (disposed) { throw new ObjectDisposedException("CabDecompressor"); } GCHandle gch = GCHandle.Alloc(fstream); try { return(CabSdk.FdiIsCabinet(FdiContext, (IntPtr)gch, cabinfo)); } finally { gch.Free(); } }
/// <summary> /// Выполняет распаковку CAB-архива /// </summary> public void Decompress() { using (CabDecompressor decompressor = new CabDecompressor()) { decompressor.NotifyCopyFile += OnCopyFile; decompressor.NotifyCloseFile += OnCloseFile; FdiCabinetInfo cabInfo = new FdiCabinetInfo(); // является ли файл CAB-архивом if (decompressor.IsCabinetFile(_cabName, cabInfo)) { // распаковка decompressor.ExtractFiles(_cabName); } } if (_deleteSourceCab) File.Delete(_cabName); }
static bool doFdiTest(string cabinetFullPath) { using (CabDecompressor decomp = new CabDecompressor()) { // setup event handlers decomp.NotifyCabinetInfo += new NotifyEventHandler(decomp_NotifyCabinetInfo); decomp.NotifyCloseFile += new NotifyEventHandler(decomp_NotifyCloseFile); decomp.NotifyCopyFile += new NotifyEventHandler(decomp_NotifyCopyFile); decomp.NotifyEnumerate += new NotifyEventHandler(decomp_NotifyEnumerate); decomp.NotifyNextCabinet += new NotifyEventHandler(decomp_NotifyNextCabinet); decomp.NotifyPartialFile += new NotifyEventHandler(decomp_NotifyPartialFile); FdiCabinetInfo cabInfo = new FdiCabinetInfo(); using (FileStream fs = new FileStream(cabinetFullPath, FileMode.Open, FileAccess.Read)) { if (!decomp.IsCabinetFile(fs, cabInfo)) { Console.WriteLine("File is not a cabinet."); return false; } #if DO_OUTPUT // The file is a cabinet. Display some info. Console.WriteLine( "Information on cabinet file '{0}'\n" + " Total length of cabinet file : {1}\n" + " Number of folders in cabinet : {2}\n" + " Number of files in cabinet : {3}\n" + " Cabinet set ID : {4}\n" + " Cabinet number in set : {5}\n" + " RESERVE area in cabinet? : {6}\n" + " Chained to prev cabinet? : {7}\n" + " Chained to next cabinet? : {8}\n", cabinetFullPath, cabInfo.Length, cabInfo.FolderCount, cabInfo.FileCount, cabInfo.SetId, cabInfo.CabinetNumber, cabInfo.HasReserve, cabInfo.HasPrev, cabInfo.HasNext); #endif } // extract path and filename string cabinetName = Path.GetFileName(cabinetFullPath); string cabinetPath = Path.GetDirectoryName(cabinetFullPath); if (cabinetPath != string.Empty) cabinetPath = cabinetPath + Path.DirectorySeparatorChar; // let's copy some files! if (!decomp.ExtractFiles(cabinetFullPath)) { Console.WriteLine("FdiCopy failed. Error code {0}", decomp.ErrorInfo.FdiErrorCode); return false; } return true; } }
static bool doFdiTest(string cabinetFullPath) { using (CabDecompressor decomp = new CabDecompressor()) { // setup event handlers decomp.NotifyCabinetInfo += new NotifyEventHandler(decomp_NotifyCabinetInfo); decomp.NotifyCloseFile += new NotifyEventHandler(decomp_NotifyCloseFile); decomp.NotifyCopyFile += new NotifyEventHandler(decomp_NotifyCopyFile); decomp.NotifyEnumerate += new NotifyEventHandler(decomp_NotifyEnumerate); decomp.NotifyNextCabinet += new NotifyEventHandler(decomp_NotifyNextCabinet); decomp.NotifyPartialFile += new NotifyEventHandler(decomp_NotifyPartialFile); FdiCabinetInfo cabInfo = new FdiCabinetInfo(); using (FileStream fs = new FileStream(cabinetFullPath, FileMode.Open, FileAccess.Read)) { if (!decomp.IsCabinetFile(fs, cabInfo)) { Console.WriteLine("File is not a cabinet."); return(false); } #if DO_OUTPUT // The file is a cabinet. Display some info. Console.WriteLine( "Information on cabinet file '{0}'\n" + " Total length of cabinet file : {1}\n" + " Number of folders in cabinet : {2}\n" + " Number of files in cabinet : {3}\n" + " Cabinet set ID : {4}\n" + " Cabinet number in set : {5}\n" + " RESERVE area in cabinet? : {6}\n" + " Chained to prev cabinet? : {7}\n" + " Chained to next cabinet? : {8}\n", cabinetFullPath, cabInfo.Length, cabInfo.FolderCount, cabInfo.FileCount, cabInfo.SetId, cabInfo.CabinetNumber, cabInfo.HasReserve, cabInfo.HasPrev, cabInfo.HasNext); #endif } // extract path and filename string cabinetName = Path.GetFileName(cabinetFullPath); string cabinetPath = Path.GetDirectoryName(cabinetFullPath); if (cabinetPath != string.Empty) { cabinetPath = cabinetPath + Path.DirectorySeparatorChar; } // let's copy some files! if (!decomp.ExtractFiles(cabinetFullPath)) { Console.WriteLine("FdiCopy failed. Error code {0}", decomp.ErrorInfo.FdiErrorCode); return(false); } return(true); } }