@Test public void testZipFile() throws Throwable { File testFile = File.createTempFile("testFile",".txt"); File testFileZip = File.createTempFile("testFile",".zip"); PrintWriter writer = new PrintWriter(testFile); writer.write("this is a test"); writer.close(); assertThat(testFile.exists(), is(true)); assertThat(testFileZip.exists(), is(true)); assertThat(testFileZip.length(), is(0L)); ZipUtil.zipFile(testFile, testFileZip); assertThat(testFileZip.length(), not(0L)); ZipFile zipFile = new ZipFile(testFileZip); ZipEntry zipEntry = zipFile.getEntry(testFile.getName()); assertThat(zipEntry, notNullValue()); assertThat(new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry))).readLine(), is("this is a test")); zipFile.close(); testFile.delete(); testFileZip.delete(); }
public static void zipFile(File input, File output) throws IOException { FileInputStream in = null; ZipOutputStream out = null; try { in = new FileInputStream(input); out = new ZipOutputStream(new FileOutputStream(output)); out.putNextEntry(new ZipEntry(input.getName())); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); out.closeEntry(); out.finish(); } finally{ if (in != null) { in.close(); } if (out != null) { out.close(); } } }
ZipUtil { public static void zipFile(File input, File output) throws IOException { FileInputStream in = null; ZipOutputStream out = null; try { in = new FileInputStream(input); out = new ZipOutputStream(new FileOutputStream(output)); out.putNextEntry(new ZipEntry(input.getName())); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); out.closeEntry(); out.finish(); } finally{ if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
ZipUtil { public static void zipFile(File input, File output) throws IOException { FileInputStream in = null; ZipOutputStream out = null; try { in = new FileInputStream(input); out = new ZipOutputStream(new FileOutputStream(output)); out.putNextEntry(new ZipEntry(input.getName())); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); out.closeEntry(); out.finish(); } finally{ if (in != null) { in.close(); } if (out != null) { out.close(); } } }  }
ZipUtil { public static void zipFile(File input, File output) throws IOException { FileInputStream in = null; ZipOutputStream out = null; try { in = new FileInputStream(input); out = new ZipOutputStream(new FileOutputStream(output)); out.putNextEntry(new ZipEntry(input.getName())); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); out.closeEntry(); out.finish(); } finally{ if (in != null) { in.close(); } if (out != null) { out.close(); } } }  static void zipFile(File input, File output); }
ZipUtil { public static void zipFile(File input, File output) throws IOException { FileInputStream in = null; ZipOutputStream out = null; try { in = new FileInputStream(input); out = new ZipOutputStream(new FileOutputStream(output)); out.putNextEntry(new ZipEntry(input.getName())); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); out.closeEntry(); out.finish(); } finally{ if (in != null) { in.close(); } if (out != null) { out.close(); } } }  static void zipFile(File input, File output);  }