static public void build(String inputPath, String outputPath, int maxThread) { Console.WriteLine("input:" + inputPath); Console.WriteLine("output:" + outputPath); inputPath = inputPath + "/"; outputPath = outputPath + "/"; ArrayList list = new ArrayList(); String buildVersion = null; try { buildVersion = readFromSqlite(inputPath, list); } catch (Exception e) { throw e; //Console.WriteLine(e.Message); //return; } Console.WriteLine("buildVersion:" + buildVersion); DirectoryInfo outputDir = new DirectoryInfo(outputPath); if (outputDir.Exists) { outputDir.Delete(true); } var outputFilePath = outputPath + "/" + buildVersion + "/"; FileUtil.copyDirectoryStruct(inputPath, outputFilePath); ResourceInfo[] resInfoList = (ResourceInfo[])list.ToArray(typeof(ResourceInfo)); var list_info = new List<ResourceInfo>(); for (int j = 0; j < resInfoList.Length; j++) { FileInfo file = new FileInfo(inputPath + resInfoList[j].name); var ext = file.Extension.ToLower(); if (ext == ".meta" || ext == ".manifest") continue; else { list_info.Add(resInfoList[j]); } } StringBuilder sb = new StringBuilder(); StringBuilder lost = new StringBuilder(); int i = 0; int finishCount = 0; int count = 0; int c = list_info.Count; Console.Write("正在处理文件..."); var lockObj = new object(); int curThread = 0; var compresser = new LZMACompresser(); var encoding = Encoding.UTF8; var backNum = 0; var time = new Stopwatch(); time.Start(); while (true) { if (finishCount >= c) break; Thread.Sleep(5); if (curThread >= maxThread) continue; if (i >= c) continue; ResourceInfo resInfo = list_info[i]; i++; String fileName = resInfo.name; String newFileName = fileName; FileInfo file = new FileInfo(inputPath + fileName); if (file.Exists) { curThread++; var thread = new Thread(() => { var bytes = File.ReadAllBytes(inputPath + fileName); bytes = compresser.compress(bytes); File.WriteAllBytes(outputFilePath + newFileName, bytes); var md5 = MD5Utils.BytesToMD5(bytes); lock (lockObj) { sb.Append(newFileName); sb.Append(","); sb.Append(resInfo.version); sb.Append(","); sb.Append(bytes.Length); sb.Append(","); sb.Append(md5); sb.Append("\r\n"); finishCount++; count++; curThread--; } }); thread.Start(); } else { finishCount++; lost.Append(fileName); lost.Append("\r\n"); } var backStr = new StringBuilder(); for (int j = 0; j < backNum; j++) { backStr.Append("\u0008"); } var writeConsoleStr = string.Format("{0}/{1} thread:{2}/{3}", i, c, curThread, maxThread); Console.Write(backStr.ToString() + writeConsoleStr); backNum = encoding.GetBytes(writeConsoleStr).Length; } time.Stop(); if (count != 0) sb.Remove(sb.Length - 2, 2); Console.WriteLine(); Byte[] FileInfoBytes = Encoding.UTF8.GetBytes(sb.ToString()); FileUtil.writeFile(outputFilePath + "assetInfo.txt", FileInfoBytes); FileUtil.writeFile(outputFilePath + "assetInfo_compressed.txt", compresser.compress(FileInfoBytes)); FileUtil.writeFile(outputPath + "assetVersion.txt", Encoding.UTF8.GetBytes(buildVersion)); if (lost.Length != 0) { Console.WriteLine("未发现以下文件:"); Console.WriteLine(lost); } Console.WriteLine(); Console.WriteLine("已生成" + count + "个文件信息"); Console.WriteLine("耗时" + time.Elapsed.TotalSeconds + "秒"); }
static public void export(String inputPath, String outputPath, CompressOption op, String prefix_primaryKey, String prefix_IgnoreSheet, String prefix_IgnoreLine, String prefix_IgnoreColumn, Boolean ignoreBlank, Boolean merge) { if (Directory.Exists(inputPath)) { DirectoryInfo di = new DirectoryInfo(inputPath); FileInfo[] fileInfos = di.GetFiles("*.xls", SearchOption.AllDirectories); for (int k = 0; k < fileInfos.Length; k++) { //如果不是隐藏文件 则解析 if ((fileInfos[k].Attributes & FileAttributes.Hidden) == 0) { export(fileInfos[k].FullName, outputPath, op, prefix_primaryKey, prefix_IgnoreSheet, prefix_IgnoreLine, prefix_IgnoreColumn, ignoreBlank, merge); } } } else { KTable[] sheets = doExport(inputPath, prefix_primaryKey, prefix_IgnoreSheet, prefix_IgnoreLine, prefix_IgnoreColumn, ignoreBlank, merge); if (null == outputPath || "" == outputPath) { FileInfo fi = new FileInfo(inputPath); outputPath = fi.DirectoryName; } if (!Directory.Exists(outputPath)) throw new Exception("导出路径\"" + outputPath + "\"不存在"); foreach (KTable sheet in sheets) { packager.reset(); packager.writeTable(sheet); String path = outputPath + "/" + sheet.name + ".kk"; Byte[] bytes = packager.data; MemoryStream inStream = new MemoryStream(bytes); MemoryStream outStream = new MemoryStream(); ICompresser compresser; switch (op) { case CompressOption.lzma: compresser = new LZMACompresser(); compresser.compress(inStream, outStream); break; case CompressOption.zlib: compresser = new ZlibCompresser(); compresser.compress(inStream, outStream); break; case CompressOption.none: outStream = inStream; break; default: throw new Exception(); } FileStream fs = File.Create(path); outStream.WriteTo(fs); fs.Close(); outStream.Dispose(); } } }