private void buttonConvert_Click(object sender, EventArgs e) { CompressorEventArgs arg = new CompressorEventArgs(); arg.Folder = this.textBoxWebRoot.Text.Trim(); arg.OutoutFile = this.textBoxOutput.Text.Trim(); arg.Extensions = this.textBoxExtensions.Text.Trim(); if (String.IsNullOrEmpty(arg.Folder) || String.IsNullOrEmpty(arg.OutoutFile)) { return; } _compressThread = new Thread(ThreadCompressProc); _compressThread.IsBackground = true; _compressThread.Name = "Compressor Thread"; _compressThread.Start(arg); }
private void ThreadCompressProc(object obj) { CompressorEventArgs e = (CompressorEventArgs)obj; String rootFolder = e.Folder.Replace("\\", "/"); if (rootFolder.EndsWith("/")) { rootFolder = rootFolder.Substring(0, rootFolder.Length - 1); } int nPrefixLen = rootFolder.Length; string[] exts = e.Extensions.Replace("*", "").Split(new char[] { ' ', ';', ',' }, StringSplitOptions.RemoveEmptyEntries); string[] aryFiles = Directory.GetFiles(e.Folder, "*.*", SearchOption.AllDirectories); int nHeaderSize = (int)System.Runtime.InteropServices.Marshal.SizeOf(typeof(FileHeader)); List <String> gzipExts = exts.ToList(); List <String> files = new List <string>(); foreach (string path in aryFiles) { files.Add(path.Replace("\\", "/")); } files.Sort(CompareTwoPath); Dictionary <string, string> gzipFiles = new Dictionary <string, string>(); String gzipFolder = AppDomain.CurrentDomain.BaseDirectory + "gzip" + Path.DirectorySeparatorChar; if (!Directory.Exists(gzipFolder)) { Directory.CreateDirectory(gzipFolder); } UInt32 nTotalSize = 0; foreach (string filePath in files) { FileInfo fi = new FileInfo(filePath); long fileSize = fi.Length; if (gzipExts.IndexOf(fi.Extension) >= 0) { try { gzipFiles[filePath] = gzipFolder + (Guid.NewGuid().ToString() + ".gz"); var bytes = File.ReadAllBytes(filePath); using (FileStream fs = new FileStream(gzipFiles[filePath], FileMode.CreateNew)) using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, false)) { zipStream.Write(bytes, 0, bytes.Length); } fileSize = new FileInfo(gzipFiles[filePath]).Length; } catch (Exception er) { Debug.WriteLine(er.Message); } } if (!gzipFiles.ContainsKey(filePath)) { gzipFiles[filePath] = filePath; } nTotalSize += (UInt32)nHeaderSize; nTotalSize += (UInt32)((UInt32)((filePath.Length - nPrefixLen + 1) + 3) & 0xFFFFFFFFCUL); //4-byte alignment nTotalSize += (UInt32)((UInt32)(fileSize + 3) & 0xFFFFFFFFCUL); //4-byte alignment LogEventArgs args = new LogEventArgs(); args.Type = "Log"; args.Message = filePath; args.Timestamp = DateTime.Now; InvokeLogEvent(this, args); } nTotalSize += (UInt32)nHeaderSize; //additional zero herader int nOffset = 0; byte[] fsData = new byte[nTotalSize]; Array.Clear(fsData, 0, fsData.Length); foreach (string filePath in files) { long fileSize = new FileInfo(gzipFiles[filePath]).Length; int pathSpace = (int)((UInt32)((filePath.Length - nPrefixLen + 1) + 3) & 0xFFFFFFFFCUL); //4-byte alignment int bodySpace = (int)((UInt32)(fileSize + 3) & 0xFFFFFFFFCUL); //4-byte alignment FileInfo fi = new FileInfo(filePath); FileHeader fileIndex = new FileHeader(); fileIndex.nIndex++; fileIndex.nSize = (UInt32)fileSize; fileIndex.nAttributes |= (UInt32)ATTRI_GZIP; fileIndex.tLastModified = (UInt32)((fi.LastWriteTimeUtc.Ticks - 0x089f7ff5f7b58000) / 10000000); fileIndex.nOffsetFileData = (UInt32)(nOffset + nHeaderSize + pathSpace); fileIndex.nOffsetNextFile = (UInt32)(nOffset + nHeaderSize + pathSpace + bodySpace); IntPtr ptrHeader = Marshal.AllocHGlobal((int)nHeaderSize); Marshal.StructureToPtr(fileIndex, ptrHeader, true); Marshal.Copy(ptrHeader, fsData, nOffset, (int)nHeaderSize); Marshal.FreeHGlobal(ptrHeader); nOffset += nHeaderSize; //4-byte alignment byte[] fileName = Encoding.ASCII.GetBytes(filePath.Substring(nPrefixLen)); Buffer.BlockCopy(fileName, 0, fsData, nOffset, fileName.Length); nOffset += pathSpace; //4-byte alignment byte[] fileBody = File.ReadAllBytes(gzipFiles[filePath]); Buffer.BlockCopy(fileBody, 0, fsData, nOffset, fileBody.Length); nOffset += bodySpace; //4-byte alignment } string temp; StringBuilder fsExport = new StringBuilder(); fsExport.AppendLine("const unsigned char g_szWebRoot[] __attribute__((aligned(4))) = {"); for (int i = 0; i < fsData.Length; i++) { temp = "0x" + fsData[i].ToString("X2"); fsExport.Append(temp); if (i == fsData.Length - 1) { fsExport.AppendLine(""); } else { fsExport.Append(","); if ((i & 0x000F) == 0xF) { fsExport.AppendLine(""); } } } fsExport.AppendLine("};"); File.WriteAllText(e.OutoutFile, fsExport.ToString()); LogEventArgs evt = new LogEventArgs(); evt.Type = "Result"; evt.Message = "Success"; evt.Timestamp = DateTime.Now; InvokeLogEvent(this, evt); }