<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: to code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 00:19:30 GMT</pubDate>
    <description>DZone Snippets: to 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>
    <item>
      <title>C convert buffer to binary string</title>
      <link>http://snippets.dzone.com/posts/show/2076</link>
      <description>// Sometimes one wants to see data in binary&lt;br /&gt;// This method converts an arbitrary buffer to 1's and 0's&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;memory.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;char *getBufferAsBinaryString(void *in)&lt;br /&gt;{&lt;br /&gt;	int pos=0;&lt;br /&gt;	char result;&lt;br /&gt;	char bitstring[256];&lt;br /&gt;	memset(bitstring, 0, 256);&lt;br /&gt;	unsigned int *input= (unsigned int *)in;&lt;br /&gt;	for(int i=31;i&gt;=0;i--)&lt;br /&gt;	{&lt;br /&gt;		if (((*input &gt;&gt; i) &amp; 1)) result = '1';&lt;br /&gt;		else result = '0';&lt;br /&gt;		bitstring[pos] = result;&lt;br /&gt;		if ((i&gt;0) &amp;&amp; ((i)%4)==0)&lt;br /&gt;		{&lt;br /&gt;			pos++;&lt;br /&gt;			bitstring[pos] = ' ';&lt;br /&gt;		}&lt;br /&gt;		pos++;&lt;br /&gt;	}&lt;br /&gt;	return bitstring;&lt;br /&gt;}&lt;br /&gt;int main(int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;	int i=53003;&lt;br /&gt;	char buffer[1024];&lt;br /&gt;	char *s=getBufferAsBinaryString(&amp;i);&lt;br /&gt;	strcpy(buffer, s);&lt;br /&gt;	printf("%s\n", buffer);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;produces this output:&lt;br /&gt;0000 0000 0000 0000 1100 1111 0000 1011</description>
      <pubDate>Thu, 18 May 2006 20:54:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2076</guid>
      <author>ocorpening (Owen Corpening)</author>
    </item>
    <item>
      <title>Find conversion words</title>
      <link>http://snippets.dzone.com/posts/show/1189</link>
      <description>&lt;code&gt;&lt;br /&gt;is-conv-word?: func [word][&lt;br /&gt;	all [&lt;br /&gt;		("to-" =  copy/part to-string word 3)&lt;br /&gt;		(not error? try [&lt;br /&gt;			datatype! = type? get to-word join copy (at to-string word 4) "!"&lt;br /&gt;		])&lt;br /&gt;	]&lt;br /&gt;]&lt;br /&gt;conversion-words: does [&lt;br /&gt;	lib/ser/keep-if first system/words :is-conv-word?&lt;br /&gt;]&lt;br /&gt;print mold conversion-words&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:59:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1189</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
