最近做了一个打包工具,目的是要将J2ME做的东西作修改后重新整理打包,其中重要的还是用到SharpZipLib.dll以及生成JAD的jadmakerHelper两个类库。当然,还有参考前人使用的代码,在此因为没有记录地址就不说明了,感谢贡献。
接下来就说代码吧!
先说JAR包的META-INF/MANIFEST.MF这个文件吧,这个文件对于一个JAR包来说至关重要,要想写用中文就得用UTF-8格式写,当时还想英文了解的,后被说中国人就得有中文,说的有道理,服从:
using System; using System.Collections.Generic; using System.Text; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.GZip; namespace JARMaker { class MFFile { public static void MFCreator() { string currentpath = AppDomain.CurrentDomain.BaseDirectory; DirectoryInfo di = Directory.CreateDirectory(currentpath + "JARPackage//META-INF//"); string name = Form1.intstance.textBox_title.Text.Trim();//.Textvalue.Remove(Form1.intstance.Textvalue.IndexOf(".")); string vendor=Form1.intstance.textBox_jarname.Text.Trim(); string content = "Manifest-Version: 1.0/r/n" + "MIDlet-Vendor: "+vendor+"/r/n" + "MIDlet-Version: 1.0.0/r/n" + "MicroEdition-Configuration: CLDC-1.0/r/n" + "MIDlet-1: " + name + ",logo.png,Book/r/n" + "MIDlet-Icon: /logo.png/r/n" + "MIDlet-Name: " + name + "/r/n" + "MicroEdition-Profile: MIDP-2.0/r/n"; //Convert.Write(currentpath + "JARPackage//META-INF//MANIFEST.MF", content, Encoding.Default); Filewrier(currentpath + "JARPackage//META-INF//MANIFEST.MF", content); } private static void Filewrier(string path, string str) { byte[] contents = new UTF8Encoding(false).GetBytes(str); using (FileStream outstream = File.Create(path, contents.Length)) { outstream.Write(contents, 0, contents.Length); outstream.Close(); } } } }
其中,name就是程序的名称,也是图标下的文件,这个可是不是JAR文件名,JAR文件名不能为中文。
接下来就是打包啦!将要打包的文件统一放于一目录下,便于管理。
先借用一下前人的操作ZipLib库:
using System; using System.Collections.Generic; using System.Text; using ICSharpCode; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip.Compression; using System.IO; namespace JARMaker { public class MESZIP { private string sServerDir = ""; private string sZipFileName = ""; private string sSourceFileName = ""; public MESZIP(string sServerDir, string sZipFileName, string sSourceFileName) { this.sServerDir = sServerDir; this.sZipFileName = sZipFileName; this.sSourceFileName = sSourceFileName; } public MESZIP() { } public string ServerDir { set { sServerDir = value; } } public string ZipFileName { set { sZipFileName = value; } } public string SourceFileName { set { sSourceFileName = value; } } /// <summary> /// 壓縮文件 /// </summary> public void ZipFile() { if (!sServerDir.Equals("")) { ICSharpCode.SharpZipLib.Zip.FastZip fz = new FastZip(); fz.CreateEmptyDirectories = true; fz.CreateZip(sZipFileName, sServerDir, true, ""); if (!sZipFileName.Equals("")) { ZipOutputStream u = new ZipOutputStream(File.Create(sServerDir + "//" + sZipFileName)); this.AddZipEntry(sSourceFileName, u, out u); u.Close(); } } } public void AddZipEntry(string sSourceFileName, ZipOutputStream u, out ZipOutputStream j) { string sFileFullName = sServerDir + "//" + sSourceFileName; if (File.Exists(sFileFullName)) { u.SetLevel(6); FileStream f = File.OpenRead(sFileFullName); byte[] b = new byte[f.Length]; f.Read(b, 0, b.Length); ZipEntry z = new ZipEntry(sSourceFileName); u.PutNextEntry(z); u.Write(b, 0, b.Length); f.Close(); } j = u; } } }
有了这个后,就只需要你要处理的小细节做好就可以了,最后一步,打包:
MESZIP zip = new MESZIP(sourcepath, jarname, "*.*"); zip.ZipFile();
当然,也还可以生成JAD文件,上面有提到过的。还是老样子,先借用一个操作类:
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using jadmakerHelper; namespace JARMaker { class JADCreator { public static bool Creat(string jarpath) { int index = jarpath.LastIndexOf("//") + 1; string name = jarpath.Substring(index); string path = jarpath.Remove(index); jadMakerHelper jad = new jadMakerHelper(path, name); jad.CreateJad(); return true; } } }
一步生成JAD文件:
JADCreator.Creat(selectpath + "//" + uid[i] + "//" + jarname);
OK,JAR、JAD文件已经生成,这下可以到手机上去跑啦!
谢谢前辈的贡献!