List of usage examples for java.util.zip ZipOutputStream putNextEntry
public void putNextEntry(ZipEntry e) throws IOException
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 ww w .jav a2s. com*/ } } out.close(); } return zipfile; }
From source file:com.rackspacecloud.client.cloudfiles.sample.FilesCopy.java
public static File zipFile(File f) throws IOException { byte[] buf = new byte[1024]; int len;/*from www . ja v a 2 s . c om*/ // Create the ZIP file String filenameWithZipExt = f.getName() + ZIPEXTENSION; File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile)); FileInputStream in = new FileInputStream(f); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(f.getName())); // Transfer bytes from the file to the ZIP file while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); out.flush(); in.close(); out.close(); return zippedFile; }
From source file:net.grinder.util.LogCompressUtil.java
/** * Compress the given file./*from w w w . j av a2 s. c o 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:com.jaspersoft.studio.community.utils.CommunityAPIUtils.java
/** * Creates a ZIP file using the specified zip entries. * /*from www .ja va2s. co m*/ * @param zipEntries the list of entries that will end up in the final zip file * @return the zip file reference * @throws CommunityAPIException */ public static File createZipFile(List<ZipEntry> zipEntries) throws CommunityAPIException { String tmpDirectory = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$ String zipFileLocation = tmpDirectory; if (!(tmpDirectory.endsWith("/") || tmpDirectory.endsWith("\\"))) { //$NON-NLS-1$ //$NON-NLS-2$ zipFileLocation += System.getProperty("file.separator"); //$NON-NLS-1$ } zipFileLocation += "issueDetails.zip"; //$NON-NLS-1$ try { // create byte buffer byte[] buffer = new byte[1024]; // create object of FileOutputStream FileOutputStream fout = new FileOutputStream(zipFileLocation); // create object of ZipOutputStream from FileOutputStream ZipOutputStream zout = new ZipOutputStream(fout); for (ZipEntry ze : zipEntries) { //create object of FileInputStream for source file FileInputStream fin = new FileInputStream(ze.getLocation()); zout.putNextEntry(new java.util.zip.ZipEntry(ze.getLocation())); // After creating entry in the zip file, actually write the file. int length; while ((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } //close the zip entry and related InputStream zout.closeEntry(); fin.close(); } //close the ZipOutputStream zout.close(); } catch (IOException e) { throw new CommunityAPIException(Messages.CommunityAPIUtils_ZipCreationError, e); } return new File(zipFileLocation); }
From source file:com.compomics.pladipus.core.control.util.ZipUtils.java
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws UnspecifiedPladipusException, FileNotFoundException, IOException { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else {/* www .ja va 2 s . co m*/ byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } }
From source file:com.jaspersoft.jasperserver.jrsh.common.ZipUtil.java
protected static void addFiles(ZipOutputStream zos, String folder, String baseFolder) throws Exception { File file = new File(folder); if (file.exists()) { if (file.isDirectory()) { if (!folder.equalsIgnoreCase(baseFolder)) { String entryName = folder.substring(baseFolder.length() + 1, folder.length()) + separatorChar; ZipEntry zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); }/* ww w .j a v a2 s . co m*/ File files[] = file.listFiles(); if (files != null) { for (File f : files) { addFiles(zos, f.getAbsolutePath(), baseFolder); } } } else { String entryName = folder.substring(baseFolder.length() + 1, folder.length()); ZipEntry zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); try (FileInputStream in = new FileInputStream(folder)) { int len; byte buf[] = new byte[1024]; while ((len = in.read(buf)) > 0) { zos.write(buf, 0, len); } zos.closeEntry(); } } } }
From source file:dk.netarkivet.common.utils.ZipUtils.java
/** Zip the contents of a directory into a file. * Does *not* zip recursively.//from w ww.ja va 2 s . c o m * * @param dir The directory to zip. * @param into The (zip) file to create. The name should typically end * in .zip, but that is not required. */ public static void zipDirectory(File dir, File into) { ArgumentNotValid.checkNotNull(dir, "File dir"); ArgumentNotValid.checkNotNull(into, "File into"); ArgumentNotValid.checkTrue(dir.isDirectory(), "directory '" + dir + "' to zip is not a directory"); ArgumentNotValid.checkTrue(into.getAbsoluteFile().getParentFile().canWrite(), "cannot write to '" + into + "'"); File[] files = dir.listFiles(); FileOutputStream out; try { out = new FileOutputStream(into); } catch (IOException e) { throw new IOFailure("Error creating ZIP outfile file '" + into + "'", e); } ZipOutputStream zipout = new ZipOutputStream(out); try { try { for (File f : files) { if (f.isFile()) { ZipEntry entry = new ZipEntry(f.getName()); zipout.putNextEntry(entry); FileUtils.writeFileToStream(f, zipout); } // Not doing directories yet. } } finally { zipout.close(); } } catch (IOException e) { throw new IOFailure("Failed to zip directory '" + dir + "'", e); } }
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);//from ww w . j a v a 2s . c om 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:de.tudarmstadt.ukp.clarin.webanno.api.dao.ZipUtils.java
private static void addToZip(ZipOutputStream zip, File aBasePath, File aPath) throws IOException { if (aPath.isDirectory()) { for (File file : aPath.listFiles()) { addToZip(zip, aBasePath, file); }/* w w w . j ava 2 s . c o m*/ } else { FileInputStream in = null; try { in = new FileInputStream(aPath); String relativePath = aBasePath.toURI().relativize(aPath.toURI()).getPath(); zip.putNextEntry(new ZipEntry(relativePath)); IOUtils.copy(in, zip); } finally { closeQuietly(in); } } }
From source file:com.juancarlosroot.threads.SimulatedUser.java
public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); byte[] bytes = new byte[2048]; int length;/*from w ww . j ava2 s.com*/ while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); }