List of usage examples for java.util.zip ZipOutputStream close
public void close() throws IOException
From source file:com.ephesoft.dcma.util.FileUtils.java
/** * API to zip list of files to a desired file. Operation aborted if any file is invalid or a directory. * //from ww w . ja v a 2 s . c o m * @param filePaths {@link List}< {@link String}> * @param outputFilePath {@link String} * @throws IOException in case of error */ public static void zipMultipleFiles(List<String> filePaths, String outputFilePath) throws IOException { LOGGER.info("Zipping files to " + outputFilePath + ".zip file"); File outputFile = new File(outputFilePath); if (outputFile.exists()) { LOGGER.info(outputFilePath + " file already exists. Deleting existing and creating a new file."); outputFile.delete(); } byte[] buffer = new byte[UtilConstants.BUFFER_CONST]; // Create a buffer for copying int bytesRead; ZipOutputStream out = null; FileInputStream input = null; try { out = new ZipOutputStream(new FileOutputStream(outputFilePath)); for (String filePath : filePaths) { LOGGER.info("Writing file " + filePath + " into zip file."); File file = new File(filePath); if (!file.exists() || file.isDirectory()) { throw new Exception("Invalid file: " + file.getAbsolutePath() + ". Either file does not exists or it is a directory."); } input = new FileInputStream(file); // Stream to read file ZipEntry entry = new ZipEntry(file.getName()); // Make a ZipEntry out.putNextEntry(entry); // Store entry bytesRead = input.read(buffer); while (bytesRead != -1) { out.write(buffer, 0, bytesRead); bytesRead = input.read(buffer); } } } catch (Exception e) { LOGGER.error("Exception occured while zipping file." + e.getMessage(), e); } finally { if (input != null) { input.close(); } if (out != null) { out.close(); } } }
From source file:org.messic.server.api.APISong.java
@Transactional public void getSongsZip(User user, List<Long> desiredSongs, OutputStream os) throws IOException { ZipOutputStream zos = new ZipOutputStream(os); // level - the compression level (0-9) zos.setLevel(9);//from ww w .j av a 2 s .c om HashMap<String, String> songs = new HashMap<String, String>(); for (Long songSid : desiredSongs) { MDOSong song = daoSong.get(user.getLogin(), songSid); if (song != null) { // add file // extract the relative name for entry purpose String entryName = song.getLocation(); if (songs.get(entryName) == null) { // not repeated songs.put(entryName, "ok"); ZipEntry ze = new ZipEntry(entryName); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(song.calculateAbsolutePath(daoSettings.getSettings())); int len; byte buffer[] = new byte[1024]; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); } } } zos.close(); }
From source file:ch.randelshofer.cubetwister.HTMLExporter.java
public void exportToDirectory(String documentName, DocumentModel model, File dir, ProgressObserver p) throws IOException { this.documentName = documentName; this.model = model; this.dir = dir; this.zipFile = null; this.p = p;// w ww . ja va 2s .c o m init(); processHTMLTemplates(p); new File(dir, "applets").mkdir(); ZipOutputStream zout = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(new File(dir, "applets/resources.xml.zip")))); zout.setLevel(Deflater.BEST_COMPRESSION); try { //model.writeXML(new PrintWriter(new File(dir, "applets/resources.xml"))); zout.putNextEntry(new ZipEntry("resources.xml")); PrintWriter pw = new PrintWriter(zout); model.writeXML(pw); pw.flush(); zout.closeEntry(); } finally { zout.close(); } p.setProgress(p.getProgress() + 1); }
From source file:org.kuali.ole.docstore.common.client.DocstoreRestClient.java
public File createZipFile(File sourceDir) throws IOException { File zipFile = File.createTempFile("tmp", ".zip"); String path = sourceDir.getAbsolutePath(); ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile)); ArrayList<File> fileList = getAllFilesList(sourceDir); for (File file : fileList) { ZipEntry ze = new ZipEntry(file.getAbsolutePath().substring(path.length() + 1)); zip.putNextEntry(ze);// w ww. j a va 2s .c o m FileInputStream fis = new FileInputStream(file); org.apache.commons.compress.utils.IOUtils.copy(fis, zip); fis.close(); zip.closeEntry(); } zip.close(); return zipFile; }
From source file:com.microsoft.tfs.client.common.ui.teambuild.commands.CreateUploadZipCommand.java
/** * Copy zip file and remove ignored files * * @param srcFile/* w w w . j a v a 2 s .c o m*/ * @param destFile * @throws IOException */ public void copyZip(final String srcFile, final String destFile) throws Exception { loadDefaultExcludePattern(getRootFolderInZip(srcFile)); final ZipFile zipSrc = new ZipFile(srcFile); final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile)); try { final Enumeration<? extends ZipEntry> entries = zipSrc.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (!isExcluded(LocalPath.combine(srcFile, entry.getName()), false, srcFile)) { final ZipEntry newEntry = new ZipEntry(entry.getName()); out.putNextEntry(newEntry); final BufferedInputStream in = new BufferedInputStream(zipSrc.getInputStream(entry)); int len; final byte[] buf = new byte[65536]; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } } out.finish(); } catch (final IOException e) { errorMsg = Messages.getString("CreateUploadZipCommand.CopyArchiveErrorMessageFormat"); //$NON-NLS-1$ log.error("Exceptions when copying exising archive ", e); //$NON-NLS-1$ throw e; } finally { out.close(); zipSrc.close(); } }
From source file:dk.itst.oiosaml.sp.configuration.ConfigurationHandler.java
protected File generateZipFile(final String contextPath, final String password, byte[] idpMetadata, byte[] keystore, EntityDescriptor descriptor) throws IOException { File zipFile = File.createTempFile("oiosaml-", ".zip"); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); zos.putNextEntry(new ZipEntry("oiosaml-sp.properties")); zos.write(renderTemplate("defaultproperties.vm", new HashMap<String, Object>() { {//from w ww .jav a2s.c om put("homename", Constants.PROP_HOME); put("servletPath", contextPath); put("password", password); } }, false).getBytes()); zos.closeEntry(); zos.putNextEntry(new ZipEntry("metadata/SP/SPMetadata.xml")); zos.write(SAMLUtil.getSAMLObjectAsPrettyPrintXML(descriptor).getBytes()); zos.closeEntry(); zos.putNextEntry(new ZipEntry("metadata/IdP/IdPMetadata.xml")); zos.write(idpMetadata); zos.closeEntry(); zos.putNextEntry(new ZipEntry("certificate/keystore")); zos.write(keystore); zos.closeEntry(); zos.putNextEntry(new ZipEntry("oiosaml-sp.log4j.xml")); IOUtils.copy(getClass().getResourceAsStream("oiosaml-sp.log4j.xml"), zos); zos.closeEntry(); zos.close(); return zipFile; }
From source file:io.fabric8.maven.generator.springboot.SpringBootGenerator.java
private void copyFilesToFatJar(List<File> libs, List<File> classes, File target) throws IOException { File tmpZip = File.createTempFile(target.getName(), null); tmpZip.delete();//from w w w .ja va 2 s . c o m // Using Apache commons rename, because renameTo has issues across file systems FileUtils.moveFile(target, tmpZip); byte[] buffer = new byte[8192]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target)); for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) { if (matchesFatJarEntry(libs, ze.getName(), true) || matchesFatJarEntry(classes, ze.getName(), false)) { continue; } out.putNextEntry(ze); for (int read = zin.read(buffer); read > -1; read = zin.read(buffer)) { out.write(buffer, 0, read); } out.closeEntry(); } for (File lib : libs) { try (InputStream in = new FileInputStream(lib)) { out.putNextEntry(createZipEntry(lib, getFatJarFullPath(lib, true))); for (int read = in.read(buffer); read > -1; read = in.read(buffer)) { out.write(buffer, 0, read); } out.closeEntry(); } } for (File cls : classes) { try (InputStream in = new FileInputStream(cls)) { out.putNextEntry(createZipEntry(cls, getFatJarFullPath(cls, false))); for (int read = in.read(buffer); read > -1; read = in.read(buffer)) { out.write(buffer, 0, read); } out.closeEntry(); } } out.close(); tmpZip.delete(); }
From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * Creates a zip archive of the currently serialized files in * getCurrentDirectory(), placing the archive in getArchiveDirectory(). * * @throws RuntimeException if clazz cannot be serialized. This exception * has an informative message and wraps the * originally thrown exception as root cause. * @see #getCurrentDirectory()/*from ww w. j a va2s . c o m*/ * @see #getArchiveDirectory() */ public void archiveCurrentDirectory() throws RuntimeException { System.out.println("Making zip archive of files in " + getCurrentDirectory() + ", putting it in " + getArchiveDirectory() + "."); File current = new File(getCurrentDirectory()); if (!current.exists() || !current.isDirectory()) { throw new IllegalArgumentException("There is no " + current.getAbsolutePath() + " directory. " + "\nThis is where the serialized classes should be. " + "Please run serializeCurrentDirectory() first."); } File archive = new File(getArchiveDirectory()); if (archive.exists() && !archive.isDirectory()) { throw new IllegalArgumentException( "Output directory " + archive.getAbsolutePath() + " is not a directory."); } if (!archive.exists()) { boolean success = archive.mkdirs(); } String[] filenames = current.list(); // Create a buffer for reading the files byte[] buf = new byte[1024]; try { String version = Version.currentRepositoryVersion().toString(); // Create the ZIP file String outFilename = "serializedclasses-" + version + ".zip"; File _file = new File(getArchiveDirectory(), outFilename); FileOutputStream fileOut = new FileOutputStream(_file); ZipOutputStream out = new ZipOutputStream(fileOut); // Compress the files for (String filename : filenames) { File file = new File(current, filename); FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. ZipEntry entry = new ZipEntry(filename); entry.setSize(file.length()); entry.setTime(file.lastModified()); out.putNextEntry(entry); // 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(); System.out.println("Finished writing zip file " + outFilename + "."); } catch (IOException e) { throw new RuntimeException("There was an I/O error associated with " + "the process of zipping up files in " + getCurrentDirectory() + ".", e); } }
From source file:com.ephesoft.dcma.util.FileUtils.java
/** * This method zips the contents of Directory specified into a zip file whose name is provided. * /*ww w .j av a2 s. co m*/ * @param dir2zip {@link String} * @param zout {@link String} * @param dir2zipName {@link String} * @throws IOException in case of error */ public static void zipDirectory(String dir2zip, ZipOutputStream zout, String dir2zipName) throws IOException { File srcDir = new File(dir2zip); List<String> fileList = listDirectory(srcDir); for (String fileName : fileList) { File file = new File(srcDir.getParent(), fileName); String zipName = fileName; if (File.separatorChar != FORWARD_SLASH) { zipName = fileName.replace(File.separatorChar, FORWARD_SLASH); } zipName = zipName.substring( zipName.indexOf(dir2zipName + BACKWARD_SLASH) + 1 + (dir2zipName + BACKWARD_SLASH).length()); ZipEntry zipEntry; if (file.isFile()) { zipEntry = new ZipEntry(zipName); zipEntry.setTime(file.lastModified()); zout.putNextEntry(zipEntry); FileInputStream fin = new FileInputStream(file); byte[] buffer = new byte[UtilConstants.BUFFER_CONST]; for (int n; (n = fin.read(buffer)) > 0;) { zout.write(buffer, 0, n); } if (fin != null) { fin.close(); } } else { zipEntry = new ZipEntry(zipName + FORWARD_SLASH); zipEntry.setTime(file.lastModified()); zout.putNextEntry(zipEntry); } } if (zout != null) { zout.close(); } }