List of usage examples for java.util.zip ZipOutputStream ZipOutputStream
public ZipOutputStream(OutputStream out)
From source file:Main.java
private static byte[] createZip(Map<String, byte[]> files) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zf = new ZipOutputStream(bos); Iterator<String> it = files.keySet().iterator(); String fileName = null;/*from w ww . j a v a 2s. c o m*/ ZipEntry ze = null; while (it.hasNext()) { fileName = it.next(); ze = new ZipEntry(fileName); zf.putNextEntry(ze); zf.write(files.get(fileName)); } zf.close(); return bos.toByteArray(); }
From source file:Main.java
public static void zipMutiCompress(File[] srcfile, File destFile) { byte[] buf = new byte[BUFFERED_SIZE]; try {//from w w w. j av a 2 s. c o m ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile)); if (null != srcfile && srcfile.length > 0) { for (int i = 0; i < srcfile.length; i++) { File file = srcfile[i]; if (null != file) { FileInputStream in = new FileInputStream(file); out.putNextEntry(new ZipEntry(file.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } } } out.close(); } catch (IOException e) { } }
From source file:FolderZiper.java
static public void zipFolder(String srcFolder, String destZipFile) throws Exception { ZipOutputStream zip = null;/*w w w .j a v a2 s . co m*/ FileOutputStream fileWriter = null; fileWriter = new FileOutputStream(destZipFile); zip = new ZipOutputStream(fileWriter); addFolderToZip("", srcFolder, zip); zip.flush(); zip.close(); }
From source file:Main.java
public static ZipOutputStream createZipOutputStream(File file, int compressLevel) { try {//from w w w .java 2 s.c o m if (!file.exists()) { file.createNewFile(); } final FileOutputStream fos = new FileOutputStream(file); final ZipOutputStream zos = new ZipOutputStream(fos); zos.setLevel(compressLevel); return zos; } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:Main.java
static public File zipFolder(File srcFolder, String destZipFile) throws Exception { final File file = new File(srcFolder, destZipFile); final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file))); try {// w w w . j a v a2s.co m addFolderToZip("", srcFolder, zip, destZipFile); } finally { zip.close(); } return file; }
From source file:Main.java
public static void compressDir(File file) throws IOException { FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip"); CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32()); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs)); compressDir(file, out, file.getAbsolutePath()); out.flush();/*from ww w.j a v a 2s .c om*/ out.close(); }
From source file:Main.java
public static String compress(String filename) throws FileNotFoundException, IOException { byte[] buffer = new byte[4096]; int bytesRead; String[] entries = { ".mp4" }; String zipfile = filename.replace(".mp4", ".zip"); if (!(new File(zipfile)).exists()) { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); out.setLevel(Deflater.BEST_COMPRESSION); out.setMethod(ZipOutputStream.DEFLATED); for (int i = 0; i < entries.length; i++) { File f = new File(filename.replace(".mp4", entries[i])); if (f.exists()) { FileInputStream in = new FileInputStream(f); ZipEntry entry = new ZipEntry(f.getName()); out.putNextEntry(entry); while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close();//from w ww . j av a 2 s . co m } } out.close(); } return zipfile; }
From source file:Main.java
public static void compressAFileToZip(File zip, File source) throws IOException { Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length()); Log.d("zip:", zip.getAbsolutePath()); zip.getParentFile().mkdirs();//w w w . ja va 2 s.c o m File tempFile = new File(zip.getAbsolutePath() + ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); ZipEntry zipEntry = new ZipEntry(source.getName()); zipEntry.setTime(source.lastModified()); zos.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(source); BufferedInputStream bis = new BufferedInputStream(fis); byte[] bArr = new byte[4096]; int byteRead = 0; while ((byteRead = bis.read(bArr)) != -1) { zos.write(bArr, 0, byteRead); } zos.flush(); zos.closeEntry(); zos.close(); Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize()); bos.flush(); fos.flush(); bos.close(); fos.close(); bis.close(); fis.close(); zip.delete(); tempFile.renameTo(zip); }
From source file:Main.java
public static void zip(String zipFileName, String[] zipEntries) { try (ZipOutputStream zos = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(zipFileName)))) { // Set the compression level to best compression zos.setLevel(Deflater.BEST_COMPRESSION); for (int i = 0; i < zipEntries.length; i++) { File entryFile = new File(zipEntries[i]); if (!entryFile.exists()) { System.out.println("The entry file " + entryFile.getAbsolutePath() + " does not exist"); System.out.println("Aborted processing."); return; }/*from www. j a va 2 s .c o m*/ ZipEntry ze = new ZipEntry(zipEntries[i]); zos.putNextEntry(ze); addEntryContent(zos, zipEntries[i]); zos.closeEntry(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:Utils.java
/** * Zip a list of file into one zip file. * //from w w w . j a va 2 s . c o m * @param files * files to zip * @param targetZipFile * target zip file * @throws IOException * IO error exception can be thrown when copying ... */ public static void zipFile(final File[] files, final File targetZipFile) throws IOException { try { FileOutputStream fos = new FileOutputStream(targetZipFile); ZipOutputStream zos = new ZipOutputStream(fos); byte[] buffer = new byte[128]; for (int i = 0; i < files.length; i++) { File currentFile = files[i]; if (!currentFile.isDirectory()) { ZipEntry entry = new ZipEntry(currentFile.getName()); FileInputStream fis = new FileInputStream(currentFile); zos.putNextEntry(entry); int read = 0; while ((read = fis.read(buffer)) != -1) { zos.write(buffer, 0, read); } zos.closeEntry(); fis.close(); } } zos.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("File not found : " + e); } }