canonical example by Tabnine
public void zipFile(File srcFile, File zipFile) throws IOException { try (FileInputStream fis = new FileInputStream(srcFile); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) { zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; byte[] buffer = new byte[1024]; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } }
public static void makeEvilZip(File outputFile) throws IOException { ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outputFile)); ZipEntry zipEntry = new ZipEntry("../../../../../../../../../../../../../../../tmp/evil.txt"); zipOutputStream.putNextEntry(zipEntry); byte[] output = StringUtils.toUtf8("evil text"); zipOutputStream.write(output); zipOutputStream.closeEntry(); zipOutputStream.close(); } }
private static void doZip(String entryName, InputStream in, ZipOutputStream out) throws IOException { ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); IOUtils.copy(in, out); out.closeEntry(); }
void notBug(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException { any.putNextEntry(anyZipEntry); any.write(anyValue); any.closeEntry(); }
private static void copyToZipStream(InputStream is, ZipEntry entry, ZipOutputStream zos) throws IOException { zos.putNextEntry(entry); IOUtils.copy(is, zos); is.close(); zos.closeEntry(); }
public static long copyFileToZipStream( File file, ZipOutputStream zipOutputStream, Progressable progressable ) throws IOException { createNewZipEntry(zipOutputStream, file); long numRead = 0; try (FileInputStream inputStream = new FileInputStream(file)) { byte[] buf = new byte[0x10000]; for (int bytesRead = inputStream.read(buf); bytesRead >= 0; bytesRead = inputStream.read(buf)) { progressable.progress(); if (bytesRead == 0) { continue; } zipOutputStream.write(buf, 0, bytesRead); progressable.progress(); numRead += bytesRead; } } zipOutputStream.closeEntry(); progressable.progress(); return numRead; }
StringBuilder sb = new StringBuilder(); sb.append("Test String"); File f = new File("d:\\test.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f)); ZipEntry e = new ZipEntry("mytext.txt"); out.putNextEntry(e); byte[] data = sb.toString().getBytes(); out.write(data, 0, data.length); out.closeEntry(); out.close();
public void testZip(ZipOutputStream zos) throws Exception { ZipEntry ze = new ZipEntry("foo"); zos.putNextEntry(ze); zos.closeEntry(); }
private void writeEntry(BufferedInputStream bis, ZipEntry ze) throws IOException { byte[] buf = new byte[BUFFER_SIZE]; mZos.putNextEntry(ze); int len; while ((len = bis.read(buf, 0, BUFFER_SIZE)) != -1) { mZos.write(buf, 0, len); } mZos.closeEntry(); bis.close(); }
protected void zipFile(byte[] classBytesArray, ZipOutputStream zos, String entryName) { try { ZipEntry entry = new ZipEntry(entryName); zos.putNextEntry(entry); zos.write(classBytesArray, 0, classBytesArray.length); zos.closeEntry(); zos.flush(); } catch (Exception e) { e.printStackTrace(); } } }
private boolean createDir0(String path) throws IOException { int x = path.lastIndexOf('/', path.length() - 2); if (x > 0) { String n = path.substring(0, x + 1); createDir0(n); } if (!path.contains(path)) { files.add(path); ZipEntry zipEntry = new ZipEntry(path); zos.putNextEntry(zipEntry); zos.closeEntry(); return true; } return false; }
/** * add zip entry * * @param zipOutputStream * @param zipEntry * @param inputStream * @throws Exception */ public static void addZipEntry(ZipOutputStream zipOutputStream, ZipEntry zipEntry, InputStream inputStream) throws Exception { try { zipOutputStream.putNextEntry(zipEntry); byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB]; int length = -1; while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) { zipOutputStream.write(buffer, 0, length); zipOutputStream.flush(); } } catch (ZipException e) { // do nothing } finally { StreamUtil.closeQuietly(inputStream); zipOutputStream.closeEntry(); } }
public void dumpZip(Path exFile, String[] originalArgs) throws IOException { try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(exFile))) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos, StandardCharsets.UTF_8)); zos.putNextEntry(new ZipEntry("summary.txt")); dumpTxt0(writer, originalArgs); zos.closeEntry(); zos.flush(); } }
private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException { // First, copy the contents from the existing outFile: Enumeration<? extends ZipEntry> entries = inputFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = new ZipEntry(entries.nextElement()); // We can't reuse the compressed size because it depends on compression sizes. entry.setCompressedSize(-1); outputFile.putNextEntry(entry); // No need to create directory entries in the final apk if (! entry.isDirectory()) { BrutIO.copy(inputFile, outputFile, entry); } outputFile.closeEntry(); } }
private void addToOutputStream(ZipOutputStream output, InputStream input, ZipEntry ze) throws IOException { try { output.putNextEntry(ze); } catch (ZipException zipEx) { //This entry already exists. So, go with the first one. input.close(); return; } int numBytes; while ((numBytes = input.read(buffer)) > 0) { output.write(buffer, 0, numBytes); } output.closeEntry(); input.close(); }
@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY") public static void main(String args[]) throws Exception { ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream("foo.zip")); for (int i = 0; i < args.length; i++) { ZipEntry e = new ZipEntry(args[i]); zipfile.putNextEntry(e); zipfile.closeEntry(); } zipfile.close(); } }
private static void backupFile(ZipOutputStream out, InputStream in, String entryName) throws IOException { out.putNextEntry(new ZipEntry(entryName)); IOUtils.copyAndCloseInput(in, out); out.closeEntry(); } }
private static void copyToZipStream(File file, ZipEntry entry, ZipOutputStream zos) throws IOException { InputStream is = new FileInputStream(file); try { zos.putNextEntry(entry); byte[] arr = new byte[4096]; int read = is.read(arr); while (read > -1) { zos.write(arr, 0, read); read = is.read(arr); } } finally { try { is.close(); } finally { zos.closeEntry(); } } }
@Override public void write(OutputStream outStream) throws IOException { ZipOutputStream zip = new ZipOutputStream(outStream); for (File f : directory.listFiles()) { zip.putNextEntry(new ZipEntry(f.getName())); Files.copy(f, zip); zip.closeEntry(); zip.flush(); } zip.close(); } }
public static void addFolderToZip(ZipOutputStream zos, String path, String comment) throws IOException { while (path.length() != 0 && path.charAt(0) == '/') { path = path.substring(1); } // add folder record if (!StringUtil.endsWithChar(path, '/')) { path += '/'; } ZipEntry zipEntry = new ZipEntry(path); zipEntry.setTime(System.currentTimeMillis()); if (comment != null) { zipEntry.setComment(comment); } zipEntry.setSize(0); zipEntry.setCrc(0); zos.putNextEntry(zipEntry); zos.closeEntry(); }