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:de.spiritcroc.ownlog.FileHelper.java
private static void addFileToZip(ZipOutputStream zip, File file, String pathInZip) throws IOException { byte[] data = new byte[FILE_BUFFER]; BufferedInputStream in = null; try {/*from w w w . j av a2 s. c o m*/ in = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(pathInZip); zip.putNextEntry(entry); int size; while ((size = in.read(data, 0, FILE_BUFFER)) != -1) { zip.write(data, 0, size); } } finally { if (in != null) { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } } }
From source file:Main.java
static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile) throws Exception { if (srcFile.isDirectory()) { addFolderToZip(path, srcFile, zip, destZipFile); } else if (!srcFile.getName().equals(destZipFile)) { byte[] buf = new byte[1024]; int len;/*from w ww . ja va2 s .c o m*/ final InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); try { if (path.equals("/")) { zip.putNextEntry(new ZipEntry(srcFile.getName())); } else { zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName())); } while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } }
From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java
private static void writeZipEntry(final String entryName, final ZipOutputStream out, final InputStream in) throws IOException { try {// w ww .j a va 2s.co m ZipEntry fileEntry = new ZipEntry(entryName); out.putNextEntry(fileEntry); final byte[] buf = new byte[BUF_SIZE]; int avail; while ((avail = in.read(buf)) != -1) { out.write(buf, 0, avail); } } finally { out.flush(); out.closeEntry(); } }
From source file:com.jaspersoft.studio.community.utils.CommunityAPIUtils.java
/** * Creates a ZIP file using the specified zip entries. * //www.j av a 2 s . c o 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:gov.va.chir.tagline.dao.FileDao.java
private static void saveZipFile(final File zipFile, final File... inputFiles) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); for (File file : inputFiles) { final ZipEntry entry = new ZipEntry(file.getName()); zos.putNextEntry(entry);//from w ww . j ava2 s . c om final FileInputStream in = new FileInputStream(file); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); } zos.close(); }
From source file:com.recomdata.transmart.data.export.util.ZipUtil.java
/** * This method will bundle all the files into a zip file. * If there are 2 files with the same name, only the first file is part of the zip. * /*w w w . j av a 2s . c o m*/ * @param zipFileName * @param files * * @return zipFile absolute path * */ public static String bundleZipFile(String zipFileName, List<File> files) { File zipFile = null; Map<String, File> filesMap = new HashMap<String, File>(); if (StringUtils.isEmpty(zipFileName)) return null; try { zipFile = new File(zipFileName); if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) { zipFile = new File(zipFileName); } ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.setLevel(ZipOutputStream.DEFLATED); byte[] buffer = new byte[BUFFER_SIZE]; for (File file : files) { if (filesMap.containsKey(file.getName())) { continue; } else if (file.exists() && file.canRead()) { filesMap.put(file.getName(), file); zipOut.putNextEntry(new ZipEntry(file.getName())); FileInputStream fis = new FileInputStream(file); int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { zipOut.write(buffer, 0, bytesRead); } zipOut.flush(); zipOut.closeEntry(); } } zipOut.finish(); zipOut.close(); } catch (IOException e) { //log.error("Error while creating Zip file"); } return (null != zipFile) ? zipFile.getAbsolutePath() : null; }
From source file:org.eclipse.jubula.tools.internal.utils.ZipUtil.java
/** * This method converts a directory into a zip file * @param directory The directory to zip * @param base The directory to zip// w w w . j av a2 s . com * @param zos A ZipOutputStream * @throws IOException */ private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException { File[] files = directory.listFiles(); byte[] buffer = new byte[8192]; int read = 0; for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zip(files[i], base, zos); } else { FileInputStream in = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1)); zos.putNextEntry(entry); while (-1 != (read = in.read(buffer))) { zos.write(buffer, 0, read); } in.close(); } } }
From source file:com.plotsquared.iserver.util.FileUtils.java
/** * Add files to a zip file//from w w w.java 2 s . c o m * * @param zipFile Zip File * @param files Files to add to the zip * @param delete If the original files should be deleted * @throws Exception If anything goes wrong */ public static void addToZip(final File zipFile, final File[] files, final boolean delete) throws Exception { Assert.notNull(zipFile, files); if (!zipFile.exists()) { if (!zipFile.createNewFile()) { throw new RuntimeException("Couldn't create " + zipFile); } } final File temporary = File.createTempFile(zipFile.getName(), ""); //noinspection ResultOfMethodCallIgnored temporary.delete(); if (!zipFile.renameTo(temporary)) { throw new RuntimeException("Couldn't rename " + zipFile + " to " + temporary); } final byte[] buffer = new byte[1024 * 16]; // 16mb ZipInputStream zis = new ZipInputStream(new FileInputStream(temporary)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry e = zis.getNextEntry(); while (e != null) { String n = e.getName(); boolean no = true; for (File f : files) { if (f.getName().equals(n)) { no = false; break; } } if (no) { zos.putNextEntry(new ZipEntry(n)); int len; while ((len = zis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } e = zis.getNextEntry(); } zis.close(); for (File file : files) { InputStream in = new FileInputStream(file); zos.putNextEntry(new ZipEntry(file.getName())); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); in.close(); } zos.close(); temporary.delete(); if (delete) { for (File f : files) { f.delete(); } } }
From source file:org.esa.snap.engine_utilities.util.ZipUtils.java
private static void addToZipFile(File file, ZipOutputStream zipStream) { try (FileInputStream inputStream = new FileInputStream(file.getPath())) { ZipEntry entry = new ZipEntry(file.getName()); entry.setCreationTime(FileTime.fromMillis(file.lastModified())); zipStream.putNextEntry(entry);/*from ww w . jav a 2 s . c om*/ final byte[] readBuffer = new byte[2048]; int amountRead; int written = 0; while ((amountRead = inputStream.read(readBuffer)) > 0) { zipStream.write(readBuffer, 0, amountRead); written += amountRead; } } catch (Exception e) { SystemUtils.LOG.severe("Unable to zip " + file); } }
From source file:Main.java
/** * Zips a file at a location and places the resulting zip file at the toLocation. * <p/>//w w w . j a v a2 s . co m * Example: * zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip"); * <p/> * http://stackoverflow.com/a/14868161 */ public static void zipFileAtPath(String sourcePath, String toLocation) throws IOException { final int BUFFER = 2048; File sourceFile = new File(sourcePath); FileOutputStream fos = new FileOutputStream(toLocation); ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); if (sourceFile.isDirectory()) { zipSubFolder(zipOut, sourceFile, sourceFile.getParent().length() + 1); // ?? } else { byte data[] = new byte[BUFFER]; FileInputStream fis = new FileInputStream(sourcePath); BufferedInputStream bis = new BufferedInputStream(fis, BUFFER); String lastPathComponent = sourcePath.substring(sourcePath.lastIndexOf("/")); ZipEntry zipEntry = new ZipEntry(lastPathComponent); zipOut.putNextEntry(zipEntry); int count; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipOut.write(data, 0, count); } } zipOut.close(); }