Hi,
i´ve to write a program, which should read and write jar-files. Does anyone know how to do this?
I´m writing the program with c# and .net-framework 2.0 under Visual Studio 2008 Professional. I tried to use SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx) as an external library but that doesn´t work. I was told that jar is a little bit different from normal zip. Can anyone help me please?
Loading
Scott LyslePosted Feb 14, 2008, 11:40 PM
Well, a jar file is just a zipped file with a manifest. To give it a shot, I unzipped a jar using ICSharpCode.SharpZipLib.Zip and the following code.
using ICSharpCode.SharpZipLib.Zip; try { FastZip fz = new FastZip(); fz.ExtractZip(@"C:\Temp\junit-3.8.1\junit-3.8.1.jar", @"C:\Temp\UnzippedJars", ""); } catch { }And this code will package a zip file with a jar extension (if you need a manifest you will have to figure how to add that). This code will only package up the files in the first folder, if you need to consider subfolders you will have to factor that in too (use Directory.GetDirectories then get the files...)
try { string[] filenames = Directory.GetFiles(@"C:\Temp\UnzippedJars"); // set up a string to hold the path to the attachments folder string sTargetFolderZipPath = (@"C:\Temp\Rezipped"); using (ZipOutputStream s = new ZipOutputStream(File.Create(sTargetFolderZipPath + ".jar"))) { s.SetLevel(9); // 0 - store only to 9 - where 9 means best compression byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } } catch { throw; } }