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(); } }
FileOutputStream fout = new FileOutputStream("test.zip"); ZipOutputStream zout = new ZipOutputStream(fout); for all files { ZipEntry ze = new ZipEntry(filename); zout.putNextEntry(ze); send data to zout; zout.closeEntry(); } zout.close();
/** * Zip the file. * * @param srcFile The source of file. * @param zipFile The ZIP file. * @param comment The comment. * @return {@code true}: success<br>{@code false}: fail * @throws IOException if an I/O error has occurred */ public static boolean zipFile(final File srcFile, final File zipFile, final String comment) throws IOException { if (srcFile == null || zipFile == null) return false; ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(zipFile)); return zipFile(srcFile, "", zos, comment); } finally { if (zos != null) { zos.close(); } } }
zipOutputStream = new ZipOutputStream(new FileOutputStream(outputZipFullFilename)); if (inputZipFullFilename != null) { zipFile = new ZipFile(inputZipFullFilename); ZipEntry newZipEntry = new ZipEntry(zipEntryName); addZipEntry(zipOutputStream, newZipEntry, inputStream);
/** * Archives specified file into zip archive. * * @param file File to be archived. * @return Byte array representing zip archive. * @throws IOException In case of input/output exception. */ private byte[] zipFile(File file) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { ZipEntry entry = new ZipEntry(file.getName()); zos.putNextEntry(entry); try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) { IOUtils.copy(in, zos); } } return baos.toByteArray(); }
public void done() throws IOException { ZipOutputStream zip = null; try { zip = new ZipOutputStream(new BufferedOutputStream(destinationStream)); zip.setLevel(level); for (Map.Entry<String, File> zipDirToSourceFileEntry : toAdd.entrySet()) { File sourceFileToZip = zipDirToSourceFileEntry.getValue(); String destinationFolder = zipDirToSourceFileEntry.getKey(); zipUtil.addToZip(new ZipPath(destinationFolder), sourceFileToZip, zip, excludeRootDir); } zip.flush(); } finally { if (zip != null) { try { zip.close(); } catch (IOException e) { LOGGER.error("Failed to close the stream", e); } } } } }
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 static void zipFolders(final File folder, final File zip, final File assets, final Collection<String> doNotCompress) throws BrutException, IOException { mDoNotCompress = doNotCompress; ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zip)); zipFolders(folder, zipOutputStream); // We manually set the assets because we need to retain the folder structure if (assets != null) { processFolder(assets, zipOutputStream, assets.getPath().length() - 6); } zipOutputStream.close(); }
protected ZipBuilder(File zipFile) throws IOException { if (!FileUtil.isExistingFile(zipFile)) { FileUtil.touch(zipFile); } zos = new ZipOutputStream(new FileOutputStream(zipFile)); targetZipFile = zipFile; targetBaos = null; }
private ZipInputStream createZIPInputStream() throws IOException { // Create an in-memory ZIP output stream for use by the input stream (to avoid exceptions) ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream gos = new ZipOutputStream( baos ); gos.putNextEntry( new ZipEntry( "./test.txt" ) ); byte[] testBytes = "Test".getBytes(); gos.write( testBytes ); ByteArrayInputStream in = new ByteArrayInputStream( baos.toByteArray() ); return new ZipInputStream( in ); } }
private static void pack(ZipEntrySource[] entries, OutputStream os, boolean closeStream) { try { ZipOutputStream out = new ZipOutputStream(os); for (int i = 0; i < entries.length; i++) { addEntry(entries[i], out); } out.flush(); out.finish(); if (closeStream) { out.close(); } } catch (IOException e) { throw ZipExceptionUtil.rethrow(e); } }
@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(); } }
/** * Testing related; creates an empty archive to served being localized as hive-exec */ private URI createEmptyArchive() { try { File outputJar = new File(System.getProperty("build.test.dir"), "empty.jar"); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputJar)); zos.close(); return outputJar.toURI(); } catch (IOException e) { throw new RuntimeException(e); } }
private void backupConfigRepository(File backupDir) throws IOException { File configRepoDir = systemEnvironment.getConfigRepoDir(); try (ZipOutputStream configRepoZipStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(new File(backupDir, CONFIG_REPOSITORY_BACKUP_ZIP))))) { new DirectoryStructureWalker(configRepoDir.getAbsolutePath(), configRepoZipStream).walk(); } }
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(); } }
/** * zip list of file * * @param resFileList file(dir) list * @param zipFile output zip file * @throws IOException */ public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException { ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), TypedValue.BUFFER_SIZE)); for (File resFile : resFileList) { if (resFile.exists()) { zipFile(resFile, zipout, ""); } } if (comment != null) { zipout.setComment(comment); } zipout.close(); }
protected ZipBuilder(final File zipFile) throws IOException { if (!FileUtil.isExistingFile(zipFile)) { FileUtil.touch(zipFile); } zos = new ZipOutputStream(new FileOutputStream(zipFile)); targetZipFile = zipFile; targetBaos = null; }
public static void zipFile(File source, File target) throws IOException { final FileOutputStream fos = new FileOutputStream(target); final ZipOutputStream zos = new ZipOutputStream(fos); try { final ZipEntry ze = new ZipEntry(source.getName()); zos.putNextEntry(ze); pumpFromFile(source, zos); zos.closeEntry(); } finally { zos.close(); } }
public static void zip(final File input, final File output) throws IOException { final FileOutputStream out = new FileOutputStream(output); final ZipOutputStream zOut = new ZipOutputStream(out); try { zipFile("", input, zOut); } finally { zOut.close(); } }