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(); } }
void notBug(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException { any.putNextEntry(anyZipEntry); any.write(anyValue); any.closeEntry(); }
void addToZip(ZipPath path, File srcFile, ZipOutputStream zip, boolean excludeRootDir) throws IOException { if (srcFile.isDirectory()) { addFolderToZip(path, srcFile, zip, excludeRootDir); } else { byte[] buff = new byte[4096]; try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile))) { ZipEntry zipEntry = path.with(srcFile).asZipEntry(); zipEntry.setTime(srcFile.lastModified()); zip.putNextEntry(zipEntry); int len; while ((len = inputStream.read(buff)) > 0) { zip.write(buff, 0, len); } } } }
private void writeFile(ZipOutputStream out, String file, String content) throws IOException { out.putNextEntry(new ZipEntry(file)); out.write(content.getBytes(UTF_8)); } }
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();
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(); }
@Override public void saveClassEntry(String path, String archiveName, String qualifiedName, String entryName, String content) { String file = new File(getAbsolutePath(path), archiveName).getPath(); if (!checkEntry(entryName, file)) { return; } try { ZipOutputStream out = mapArchiveStreams.get(file); out.putNextEntry(new ZipEntry(entryName)); if (content != null) { out.write(content.getBytes(StandardCharsets.UTF_8)); } } catch (IOException ex) { String message = "Cannot write entry " + entryName + " to " + file; DecompilerContext.getLogger().writeMessage(message, ex); } }
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(); } } }
/** * 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(); } }
private void addFileToZip(ZipOutputStream zos, String filename) throws IOException { FileInputStream fis = new FileInputStream(new File(exportDirName + filename)); ZipEntry ze = new ZipEntry(filename); zos.putNextEntry(ze); int length; byte bytes[] = new byte[1024]; while ((length = fis.read(bytes)) >= 0) zos.write(bytes, 0, length); zos.closeEntry(); fis.close(); }
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(); }
public static void zipFile(String filePath, String zipPath) throws Exception{ byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream(zipPath); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze= new ZipEntry("spy.log"); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(filePath); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); //remember close it zos.close(); }
@NoWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY") void notBug(ZipOutputStream any, ZipEntry anyZipEntry, byte[] anyBytes) throws IOException { any.putNextEntry(anyZipEntry); any.write(anyBytes); any.closeEntry(); }
// simplified code for zip creation in java import java.io.*; import java.util.zip.*; public class ZipCreateExample { public static void main(String[] args) throws Exception { // input file FileInputStream in = new FileInputStream("F:/sometxt.txt"); // out put file ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip")); // name the file inside the zip file out.putNextEntry(new ZipEntry("zippedjava.txt")); // buffer size byte[] b = new byte[1024]; int count; while ((count = in.read(b)) > 0) { out.write(b, 0, count); } out.close(); in.close(); } }
@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY") void bug3(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException { any.write(anyValue); any.putNextEntry(anyZipEntry); any.closeEntry(); }
private static void zipGraphDir(File graphDirectory, File zipGraphFile) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(zipGraphFile); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream); byte[] buffer = new byte[1024]; for(File f : graphDirectory.listFiles()) { if (f.isDirectory()) continue; ZipEntry zipEntry = new ZipEntry(f.getName()); zipOutputStream.putNextEntry(zipEntry); FileInputStream fileInput = new FileInputStream(f); int len; while ((len = fileInput.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, len); } fileInput.close(); zipOutputStream.closeEntry(); } zipOutputStream.close(); }
@ExpectWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY") void bug2(ZipOutputStream any, ZipEntry anyZipEntry, int anyValue) throws IOException { any.putNextEntry(anyZipEntry); any.closeEntry(); any.write(anyValue); }
/** * This function helps to create zip files. * Caution this will override the original file. * @param outZip The ZipOutputStream where the data should be stored in * @param file The path of the file that should be added to zip. * @param name The path of the file inside the zip. * @throws Exception */ public static void addFileToZip(ZipOutputStream outZip, String file, String name) throws Exception { byte data[] = new byte[BUFFER_SIZE]; FileInputStream fi = new FileInputStream(file); BufferedInputStream inputStream = new BufferedInputStream(fi, BUFFER_SIZE); ZipEntry entry = new ZipEntry(name); outZip.putNextEntry(entry); int count; while((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) { outZip.write(data, 0, count); } inputStream.close(); }
@NoWarning("AM_CREATES_EMPTY_ZIP_FILE_ENTRY") void notBug(ZipOutputStream any, ZipEntry anyZipEntry, byte[] anyBytes, int anyOffset, int anySize) throws IOException { any.putNextEntry(anyZipEntry); any.write(anyBytes, anyOffset, anySize); any.closeEntry(); }