public void UnCompressTest() { string tmpPath = PrepareTestFiles("res-files-uncompression-test"); List <FileInfo> files = Directory.GetFiles(tmpPath).Select(f => new FileInfo(f)).ToList(); byte[] data = FileCompression.CompressFile(files, 9, false); Assert.IsNotNull(data); Assert.IsTrue(data.Length > 0); IDictionary <string, byte[]> decompressedFiles = FileCompression.Decompress(data); Assert.IsNotNull(decompressedFiles); Assert.IsTrue(decompressedFiles.Count == 2); Assert.IsTrue(decompressedFiles.ContainsKey("1.txt")); Assert.IsTrue(decompressedFiles.ContainsKey("2.txt")); char[] content1 = new string('*', 1024).ToCharArray(); content1[0] = 'z'; content1[content1.Length - 1] = 'z'; char[] content2 = new string('*', 1024).ToCharArray(); content2[0] = 'y'; content2[content2.Length - 1] = 'y'; Assert.IsTrue(Encoding.UTF8.GetString(decompressedFiles["1.txt"]) == new string(content1)); Assert.IsTrue(Encoding.UTF8.GetString(decompressedFiles["2.txt"]) == new string(content2)); Directory.Delete(tmpPath, true); }
/// <summary> /// 将一个目录下的所有文件打包为一个KPP资源包 /// </summary> /// <param name="folderPath">资源路径</param> /// <param name="kppDestinationFilePath">要存放的KPP资源文件地址</param> /// <param name="head">KPP资源文件头</param> /// <param name="isCompletedEnvironment">一个值,标示了当前要封装的包裹是否按照完整运行时所需要的所有依赖文件来包装</param> /// <param name="sections">KPP数据节集合</param> /// <exception cref="FolderNotFoundException">目标文件夹不存在</exception> public static void Pack(string folderPath, string kppDestinationFilePath, KPPDataHead head, bool isCompletedEnvironment = true, params IKPPDataResource[] sections) { if (!Directory.Exists(folderPath)) { throw new FolderNotFoundException("#Target folder path couldn't be find."); } if (File.Exists(kppDestinationFilePath)) { File.Delete(kppDestinationFilePath); } #region Procedure of handling non completed runtime package. if (!isCompletedEnvironment) { string[] tempFiles = Directory.GetFiles(folderPath); foreach (string file in tempFiles) { string lowerCaseFile = file.ToLower(); foreach (string globalFile in _frameworkFiles) { if (lowerCaseFile.Contains(globalFile)) { File.Delete(file); break; } } } } #endregion //handles ZIP stream. List <FileInfo> files = Directory.GetFiles(folderPath).Select(f => new FileInfo(f)).ToList(); byte[] data = FileCompression.CompressFile(files, 9, false); Crc32 crc = new Crc32(); crc.Reset(); crc.Update(data); //sets ref value. head.SetField("TotalSize", (ulong)data.Length); head.SetField("SectionCount", (ushort)sections.Length); head.SetField("CRC", crc.Value); MemoryStream stream = new MemoryStream(); head.Pack(stream); for (int i = 0; i < sections.Length; i++) { sections[i].Pack(stream); } stream.Write(data, 0, data.Length); if (File.Exists(kppDestinationFilePath)) { File.Delete(kppDestinationFilePath); } using (FileStream fStream = new FileStream(kppDestinationFilePath, FileMode.CreateNew)) { byte[] buffer = stream.ToArray(); data = new byte[stream.Length]; Buffer.BlockCopy(buffer, 0, data, 0, data.Length); fStream.Write(data, 0, data.Length); } }
public void CompressTest() { string tmpPath = PrepareTestFiles("res-files-compression-test"); List <FileInfo> files = Directory.GetFiles(tmpPath).Select(f => new FileInfo(f)).ToList(); byte[] data = FileCompression.CompressFile(files, 9, false); Assert.IsNotNull(data); Assert.IsTrue(data.Length > 0); Directory.Delete(tmpPath, true); }
public static void Main(string[] args) { var options = new Options(); if (Parser.Default.ParseArguments(args, options)) { switch (options.Action) { case Options.Actions.Compress: FileCompression.CompressFile(options.InputFile, options.OutputFile); Console.WriteLine("Complete."); break; case Options.Actions.Decompress: FileCompression.ExtractFile(options.InputFile, options.OutputFile); Console.WriteLine("Complete."); break; default: Console.WriteLine("Error, you shouldn't see this. Contact the developer."); break; } } }