// Take a tree of files starting in a directory in a zip file // and copy them to a disk directory, recreating the tree. private int unpackZipFile( File inZipFile, String directory, String parent, boolean suppressFirstPathElement) { int count = 0; if (!inZipFile.exists()) return count; parent = parent.trim(); if (!parent.endsWith(File.separator)) parent += File.separator; if (!directory.endsWith(File.separator)) directory += File.separator; File outFile = null; try { ZipFile zipFile = new ZipFile(inZipFile); Enumeration zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); String name = entry.getName().replace('/', File.separatorChar); if (name.startsWith(directory)) { if (suppressFirstPathElement) name = name.substring(directory.length()); outFile = new File(parent + name); // Create the directory, just in case if (name.indexOf(File.separatorChar) >= 0) { String p = name.substring(0, name.lastIndexOf(File.separatorChar) + 1); File dirFile = new File(parent + p); dirFile.mkdirs(); } if (!entry.isDirectory()) { System.out.println("Installing " + outFile); // Copy the file BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(entry)); int size = 1024; int n = 0; byte[] b = new byte[size]; while ((n = in.read(b, 0, size)) != -1) out.write(b, 0, n); in.close(); out.flush(); out.close(); // Count the file count++; } } } zipFile.close(); } catch (Exception e) { System.err.println("...an error occured while installing " + outFile); e.printStackTrace(); System.err.println("Error copying " + outFile.getName() + "\n" + e.getMessage()); return -count; } System.out.println(count + " files were installed."); return count; }
public static void main(String[] args) throws IOException { int servPort = Integer.parseInt(args[0]); String ksName = "keystore.jks"; char ksPass[] = "password".toCharArray(); char ctPass[] = "password".toCharArray(); try { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(ksName), ksPass); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, ctPass); SSLContext sc = SSLContext.getInstance("SSL"); sc.init(kmf.getKeyManagers(), null, null); SSLServerSocketFactory ssf = sc.getServerSocketFactory(); SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(servPort); while (true) { // Listen for a TCP connection request. SSLSocket c = (SSLSocket) s.accept(); BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream(), 1024); BufferedInputStream in = new BufferedInputStream(c.getInputStream(), 1024); byte[] byteBuffer = new byte[1024]; int count = 0; while ((byteBuffer[count] = (byte) in.read()) != -2) { count++; } String newFile = new String(byteBuffer, 0, count, "US-ASCII"); FileOutputStream writer = new FileOutputStream(newFile.trim()); int buffSize = 0; while ((buffSize = in.read(byteBuffer, 0, 1024)) != -1) { int index = 0; if ((index = (new String(byteBuffer, 0, buffSize, "US-ASCII")) .indexOf("------MagicStringCSE283Miami")) == -1) { writer.write(byteBuffer, 0, buffSize); } else { writer.write(byteBuffer, 0, index); break; } } writer.flush(); writer.close(); ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(out)); FileInputStream fin = new FileInputStream(newFile.trim()); BufferedInputStream origin = new BufferedInputStream(fin, 1024); ZipEntry entry = new ZipEntry(newFile.trim()); outZip.putNextEntry(entry); byteBuffer = new byte[1024]; int bytes = 0; while ((bytes = origin.read(byteBuffer, 0, 1024)) != -1) { outZip.write(byteBuffer, 0, bytes); } origin.close(); outZip.flush(); outZip.close(); out.flush(); out.close(); } } catch (Exception e) { System.err.println(e.toString()); } }