<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: java code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 21:10:02 GMT</pubDate>
    <description>DZone Snippets: java code</description>
    <item>
      <title>Adding files to an existing jar file</title>
      <link>http://snippets.dzone.com/posts/show/3468</link>
      <description>Adds files to an existing Zip file. &lt;br /&gt;&lt;br /&gt;Overwrites zip entries with the same name as one of the new files. &lt;br /&gt;&lt;br /&gt;The function renames the existing zip file to a temporary file and then adds all entries in the existing zip along with the new files, excluding the zip entries that have the same name as one of the new files. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note: I'm not sure I used the best way to get a temp file. A snippet for that is welcome :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;	&lt;br /&gt;	public static void addFilesToExistingZip(File zipFile,&lt;br /&gt;			 File[] files) throws IOException {&lt;br /&gt;                // get a temp file&lt;br /&gt;		File tempFile = File.createTempFile(zipFile.getName(), null);&lt;br /&gt;                // delete it, otherwise you cannot rename your existing zip to it.&lt;br /&gt;		tempFile.delete();&lt;br /&gt;&lt;br /&gt;		boolean renameOk=zipFile.renameTo(tempFile);&lt;br /&gt;		if (!renameOk)&lt;br /&gt;		{&lt;br /&gt;			throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());&lt;br /&gt;		}&lt;br /&gt;		byte[] buf = new byte[1024];&lt;br /&gt;		&lt;br /&gt;		ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));&lt;br /&gt;		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));&lt;br /&gt;		&lt;br /&gt;		ZipEntry entry = zin.getNextEntry();&lt;br /&gt;		while (entry != null) {&lt;br /&gt;			String name = entry.getName();&lt;br /&gt;			boolean notInFiles = true;&lt;br /&gt;			for (File f : files) {&lt;br /&gt;				if (f.getName().equals(name)) {&lt;br /&gt;					notInFiles = false;&lt;br /&gt;					break;&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			if (notInFiles) {&lt;br /&gt;				// Add ZIP entry to output stream.&lt;br /&gt;				out.putNextEntry(new ZipEntry(name));&lt;br /&gt;				// Transfer bytes from the ZIP file to the output file&lt;br /&gt;				int len;&lt;br /&gt;				while ((len = zin.read(buf)) &gt; 0) {&lt;br /&gt;					out.write(buf, 0, len);&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			entry = zin.getNextEntry();&lt;br /&gt;		}&lt;br /&gt;		// Close the streams		&lt;br /&gt;		zin.close();&lt;br /&gt;		// Compress the files&lt;br /&gt;		for (int i = 0; i &lt; files.length; i++) {&lt;br /&gt;			InputStream in = new FileInputStream(files[i]);&lt;br /&gt;			// Add ZIP entry to output stream.&lt;br /&gt;			out.putNextEntry(new ZipEntry(files[i].getName()));&lt;br /&gt;			// Transfer bytes from the file to the ZIP file&lt;br /&gt;			int len;&lt;br /&gt;			while ((len = in.read(buf)) &gt; 0) {&lt;br /&gt;				out.write(buf, 0, len);&lt;br /&gt;			}&lt;br /&gt;			// Complete the entry&lt;br /&gt;			out.closeEntry();&lt;br /&gt;			in.close();&lt;br /&gt;		}&lt;br /&gt;		// Complete the ZIP file&lt;br /&gt;		out.close();&lt;br /&gt;		tempFile.delete();&lt;br /&gt;	}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 07 Feb 2007 14:00:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3468</guid>
      <author>jknight (Andrian)</author>
    </item>
  </channel>
</rss>
