List of usage examples for java.util.zip ZipOutputStream write
public synchronized void write(byte[] b, int off, int len) throws IOException
From source file:com.pieframework.runtime.utils.Zipper.java
public static void zip(String zipFile, Map<String, File> flist) { byte[] buf = new byte[1024]; try {//from ww w.ja v a2s. c om // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); // Compress the files for (String url : flist.keySet()) { FileInputStream in = new FileInputStream(flist.get(url).getPath()); // Add ZIP entry to output stream. Zip entry should be relative out.putNextEntry(new ZipEntry(url)); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (Exception e) { throw new RuntimeException("Encountered errors zipping file " + zipFile, e); } }
From source file:com.amalto.core.jobox.util.JoboxUtil.java
private static void zip(File file, String zipPath, ZipOutputStream zos) throws IOException { FileInputStream is = null;// w w w . jav a 2 s . co m try { byte[] buf = new byte[1024]; // Add ZIP entry to output stream. zos.putNextEntry(new ZipEntry(zipPath)); // Transfer bytes from the file to the ZIP file int len; is = new FileInputStream(file); while ((len = is.read(buf)) > 0) { zos.write(buf, 0, len); } } finally { IOUtils.closeQuietly(is); } }
From source file:de.fu_berlin.inf.dpp.netbeans.feedback.ErrorLogManager.java
/** * Convenience wrapper method to upload an error log file to the server. To * save time and storage space, the log is compressed to a zip archive with * the given zipName./*from ww w .ja v a 2 s . co m*/ * * @param zipName * a name for the zip archive, e.g. with added user ID to make it * unique, zipName must be at least 3 characters long! * @throws IOException * if an I/O error occurs */ private static void uploadErrorLog(String zipName, File file, IProgressMonitor monitor) throws IOException { if (ERROR_LOG_UPLOAD_URL == null) { log.warn("error log upload url is not configured, cannot upload error log file"); return; } File archive = new File(System.getProperty("java.io.tmpdir"), zipName + ".zip"); ZipOutputStream out = null; FileInputStream in = null; byte[] buffer = new byte[8192]; try { in = new FileInputStream(file); out = new ZipOutputStream(new FileOutputStream(archive)); out.putNextEntry(new ZipEntry(file.getName())); int read; while ((read = in.read(buffer)) > 0) out.write(buffer, 0, read); out.finish(); out.close(); FileSubmitter.uploadFile(archive, ERROR_LOG_UPLOAD_URL, monitor); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); archive.delete(); } }
From source file:Main.java
private static void zipDir(String dir, ZipOutputStream out) throws IOException { File directory = new File(dir); URI base = directory.toURI(); ArrayList<File> filesToZip = new ArrayList<File>(); GetFiles(directory, filesToZip);/* w w w .j a v a2 s. c o m*/ for (int i = 0; i < filesToZip.size(); ++i) { FileInputStream in = new FileInputStream(filesToZip.get(i)); String name = base.relativize(filesToZip.get(i).toURI()).getPath(); out.putNextEntry(new ZipEntry(name)); byte[] buf = new byte[4096]; int bytes = 0; while ((bytes = in.read(buf)) != -1) { out.write(buf, 0, bytes); } out.closeEntry(); in.close(); } out.finish(); out.flush(); }
From source file:com.dm.material.dashboard.candybar.helpers.FileHelper.java
public static void createZip(@NonNull List<String> files, String directory) { try {//from www . j a va 2 s. co m BufferedInputStream origin; FileOutputStream dest = new FileOutputStream(directory); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; for (int i = 0; i < files.size(); i++) { FileInputStream fi = new FileInputStream(files.get(i)); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(files.get(i).substring(files.get(i).lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch (Exception e) { LogUtil.e(Log.getStackTraceString(e)); } }
From source file:com.chinamobile.bcbsp.fault.tools.Zip.java
/** * Compress files to *.zip.//w ww. java 2 s . c o m * @param fileName * file name to compress * @return compressed file. */ public static String compress(String fileName) { String targetFile = null; File sourceFile = new File(fileName); Vector<File> vector = getAllFiles(sourceFile); try { if (sourceFile.isDirectory()) { targetFile = fileName + ".zip"; } else { char ch = '.'; targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip"; } BufferedInputStream bis = null; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile)); ZipOutputStream zipos = new ZipOutputStream(bos); byte[] data = new byte[BUFFER]; if (vector.size() > 0) { for (int i = 0; i < vector.size(); i++) { File file = vector.get(i); zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file))); bis = new BufferedInputStream(new FileInputStream(file)); int count; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipos.write(data, 0, count); } bis.close(); zipos.closeEntry(); } zipos.close(); bos.close(); return targetFile; } else { File zipNullfile = new File(targetFile); zipNullfile.getParentFile().mkdirs(); zipNullfile.mkdir(); return zipNullfile.getAbsolutePath(); } } catch (IOException e) { LOG.error("[compress]", e); return "error"; } }
From source file:org.bonitasoft.engine.io.IOUtil.java
private static void copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file) throws IOException { int bytesIn;// w w w.jav a 2 s .c o m try (FileInputStream fis = new FileInputStream(file)) { while ((bytesIn = fis.read(readBuffer)) != -1) { zos.write(readBuffer, 0, bytesIn); } } }
From source file:Compress.java
/** Zip the contents of the directory, and save it in the zipfile */ public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException { // Check that the directory is a directory, and get its contents File d = new File(dir); if (!d.isDirectory()) throw new IllegalArgumentException("Compress: not a directory: " + dir); String[] entries = d.list();//from w ww. jav a2 s .c o m byte[] buffer = new byte[4096]; // Create a buffer for copying int bytes_read; // Create a stream to compress data and write it to the zipfile ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); // Loop through all entries in the directory for (int i = 0; i < entries.length; i++) { File f = new File(d, entries[i]); if (f.isDirectory()) continue; // Don't zip sub-directories FileInputStream in = new FileInputStream(f); // Stream to read file ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry out.putNextEntry(entry); // Store entry while ((bytes_read = in.read(buffer)) != -1) // Copy bytes out.write(buffer, 0, bytes_read); in.close(); // Close input stream } // When we're done with the whole loop, close the output stream out.close(); }
From source file:net.grinder.util.LogCompressUtil.java
/** * Compress the given file.// w w w. ja va 2 s .co m * * @param logFile * file to be compressed * @return compressed file byte array */ public static byte[] compressFile(File logFile) { FileInputStream fio = null; ByteArrayOutputStream out = null; ZipOutputStream zos = null; try { fio = new FileInputStream(logFile); out = new ByteArrayOutputStream(); zos = new ZipOutputStream(out); ZipEntry zipEntry = new ZipEntry("log.txt"); zipEntry.setTime(new Date().getTime()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[COMPRESS_BUFFER_SIZE]; int count = 0; while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) { zos.write(buffer, 0, count); } zos.closeEntry(); zos.finish(); zos.flush(); return out.toByteArray(); } catch (IOException e) { LOGGER.error("Error occurs while compress {}", logFile.getAbsolutePath()); LOGGER.error("Details", e); return null; } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(fio); IOUtils.closeQuietly(out); } }
From source file:org.apache.oodt.product.handlers.ofsn.util.OFSNUtils.java
public static File buildZipFile(String zipFileFullPath, File[] files) { // Create a buffer for reading the files byte[] buf = new byte[INT]; ZipOutputStream out = null; try {// w w w . j a va 2s . c om // Create the ZIP file out = new ZipOutputStream(new FileOutputStream(zipFileFullPath)); for (File file : files) { FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(file.getName())); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } } catch (IOException e) { LOG.log(Level.SEVERE, e.getMessage()); } finally { if (out != null) { try { out.close(); } catch (Exception ignore) { } } } return new File(zipFileFullPath); }