- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using CabWrapper;
- using CabDotNet;
- namespace testfci
- {
- class Class1
- {
- // When a CAB file reaches this size, a new CAB will be created
- // automatically. This is useful for fitting CAB files onto disks.
- //
- // If you want to create just one huge CAB file with everything in
- // it, change this to a very very large number.
- private const int MediaSize = 300000;
- // When a folder has this much compressed data inside it,
- // automatically flush the folder.
- //
- // Flushing the folder hurts compression a little bit, but
- // helps random access significantly.
- private const int FolderThreshold = 900000;
- // Compression type to use
- private const FciCompression CompressionType = FciCompression.MsZip;
- // Our internal state
- //
- // The FCI APIs allow us to pass back a state pointer of our own
- internal class ClientState
- {
- internal int totalCompressedSize; /* total compressed size so far */
- internal int totalUncompressedSize; /* total uncompressed size so far */
- }
- [STAThread]
- static void Main(string[] args)
- {
- if (args.Length == 0)
- {
- Console.WriteLine("TESTFCI - Demonstrates how to use the FCI library API calls\\\\n");
- Console.WriteLine("Usage: TESTFCI file1 [+] [file2] [+] [file3] ...\\\\n");
- Console.WriteLine("Where <file1>...<fileN> are input files, and a + forces a folder boundary)");
- Console.WriteLine("Output cabinets are named C:\\\\\\\\TEST1.CAB, C:\\\\\\\\TEST2.CAB, ...\\\\n");
- Console.WriteLine(" e.g. testfci test.dat source.c test.c + baloon.bmp cabbage.bmp\\\\n");
- return;
- }
- if (doFciTest(args))
- Console.WriteLine(@"TESTFCI was successful, cabinets created on C:\\\\");
- else
- Console.WriteLine("TESTFCI failed");
- }
- static bool doFciTest(string[] args)
- {
- // our client state
- ClientState cs = new ClientState();
- cs.totalCompressedSize = 0;
- cs.totalUncompressedSize = 0;
- bool returnResult = true;
- CabCompressor cabComp = new CabCompressor();
- try
- {
- setCabParameters(cabComp);
- cabComp.UserData = cs;
- // setup event handlers
- cabComp.NextCabinet += new NextCabinetEventHandler(cabComp_NextCabinet);
- cabComp.FilePlaced += new FilePlacedEventHandler(cabComp_FilePlaced);
- cabComp.FileAdded += new ProgressEventHandler(cabComp_FileAdded);
- cabComp.FolderComplete += new ProgressEventHandler(cabComp_FolderComplete);
- cabComp.CabinetComplete += new ProgressEventHandler(cabComp_CabinetComplete);
- // add each file
- foreach (string filename in args)
- {
- // Flush the folder?
- if (filename == "+")
- {
- cabComp.FlushFolder();
- // continue with next file name
- continue;
- }
- // Don't store path information in the cabinet file
- string strippedName = Path.GetFileName(filename);
- cabComp.AddFile(filename, strippedName, false, CompressionType);
- }
- // All done adding files. Flush the cabinet.
- // This will automatically flush the folder first.
- cabComp.FlushCabinet();
- Console.Write(" \\\\r");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- returnResult = false;
- }
- finally
- {
- cabComp.Dispose();
- }
- return returnResult;
- }
- static void setCabParameters(CabCompressor cabComp)
- {
- cabComp.CabInfo.MaxCabinetSize = MediaSize;
- cabComp.CabInfo.MaxFolderSize = FolderThreshold;
- // Don't reserve space for any extensions
- cabComp.CabInfo.HeaderReserve = 0;
- cabComp.CabInfo.FolderReserve = 0;
- cabComp.CabInfo.DataReserve = 0;
- // We use this to create the cabinet name
- cabComp.CabInfo.CabinetNumber = 1;
- // If you want to use disk names, use this to count disks
- cabComp.CabInfo.DiskNumber = 0;
- // Choose your own SetId
- cabComp.CabInfo.SetId = 12345;
- // Only important if CABs are spanning multiple disks,
- // in which case you will want to use a real disk name.
- //
- // Can be left as an empty string.
- cabComp.CabInfo.DiskName = "MyDisk";
- // where to store the created CAB files
- cabComp.CabInfo.CabPath = @"e:\\\\cabdotnet\\\\trash\\\\";
- // store name of first CAB file
- cabComp.CabInfo.CabName = storeCabName(cabComp.CabInfo.CabinetNumber);
- }
- static string storeCabName(int iCab)
- {
- return string.Format("TEST{0}.CAB", iCab);
- }
- public static void cabComp_NextCabinet(object sender, NextCabinetEventArgs e)
- {
- // Cabinet counter has been incremented already by FCI
- // Store next cabinet name
- string cabName = storeCabName(e.CabInfo.CabinetNumber);
- e.CabInfo.CabName = cabName;
- // You could change the disk name here too, if you wanted
- e.Result = true;
- }
- public static void cabComp_FilePlaced(object sender, FilePlacedEventArgs e)
- {
- Console.WriteLine(" placed file '{0}' (size {1}) on cabinet '{2}'",
- e.FileName, e.FileSize, e.CabInfo.CabName);
- if (e.Continuation)
- Console.WriteLine(" (Above file is a later segment of a continued file)");
- e.Result = 0;
- }
- public static void cabComp_FileAdded(object sender, ProgressEventArgs e)
- {
- ClientState cs = (ClientState)e.UserData;
- cs.totalCompressedSize += e.CompressedBlockSize;
- cs.totalUncompressedSize += e.UncompressedBlockSize;
- Console.Write(
- "Compressing: {0} -> {1} \\\\r",
- cs.totalUncompressedSize,
- cs.totalCompressedSize
- );
- e.Result = 0;
- }
- public static void cabComp_FolderComplete(object sender, ProgressEventArgs e)
- {
- int percentage = GetPercentage(e.FolderBytesCopied, e.FolderSize);
- Console.Write("Copying folder to cabinet: {0}% \\\\r", percentage);
- e.Result = 0;
- }
- private static int GetPercentage(int a, int b)
- {
- if (b == 0)
- return 0;
- return ((a*100)/b);
- }
- public static void cabComp_CabinetComplete(object sender, ProgressEventArgs e)
- {
- Console.Write("Estimated cabinet size = {0}. Actual size = {1}", e.EstimatedCabSize, e.ActualCabSize);
- e.Result = 0;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/290920136240.html
来源: http://www.codesnippet.cn/detail/290920136240.html