List of usage examples for java.util.zip ZipOutputStream close
public void close() throws IOException
From source file:com.webautomation.ScreenCaptureHtmlUnitDriver.java
public static byte[] createZip(Map<String, byte[]> files) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zipfile = new ZipOutputStream(bos); Iterator<String> i = files.keySet().iterator(); String fileName = null;/*from w w w . j a v a2 s . c o m*/ ZipEntry zipentry = null; while (i.hasNext()) { fileName = i.next(); zipentry = new ZipEntry(fileName); zipfile.putNextEntry(zipentry); zipfile.write(files.get(fileName)); } zipfile.close(); return bos.toByteArray(); }
From source file:ZipHelper.java
public static void fileToZip(File file, File zipFile, int compressionLevel) throws Exception { zipFile.createNewFile();/*from w w w . j a v a 2s.c o m*/ FileOutputStream fout = new FileOutputStream(zipFile); ZipOutputStream zout = null; try { zout = new ZipOutputStream(new BufferedOutputStream(fout)); zout.setLevel(compressionLevel); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) fileToZip(files[i], zout, file); } else if (file.isFile()) { fileToZip(file, zout, file.getParentFile()); } } finally { if (zout != null) zout.close(); } }
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("Not a directory: " + dir); String[] entries = d.list();/*from w w w . ja va 2 s. c o m*/ byte[] buffer = new byte[4096]; // Create a buffer for copying int bytesRead; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); for (int i = 0; i < entries.length; i++) { File f = new File(d, entries[i]); if (f.isDirectory()) continue;//Ignore directory FileInputStream in = new FileInputStream(f); // Stream to read file ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry out.putNextEntry(entry); // Store entry while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); } out.close(); }
From source file:azkaban.common.utils.Utils.java
public static void zip(File input, File output) throws IOException { FileOutputStream out = new FileOutputStream(output); ZipOutputStream zOut = new ZipOutputStream(out); zipFile("", input, zOut); zOut.close(); }
From source file:com.cenrise.test.azkaban.Utils.java
public static void zip(final File input, final File output) throws IOException { final FileOutputStream out = new FileOutputStream(output); final ZipOutputStream zOut = new ZipOutputStream(out); try {//from www . ja v a 2s . c o m zipFile("", input, zOut); } finally { zOut.close(); } }
From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java
private static byte[] createZippedPassAndReturnAsByteArray(final File tempPassDir) throws IOException { ByteArrayOutputStream byteArrayOutputStreamForZippedPass = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStreamForZippedPass); zip(tempPassDir, tempPassDir, zipOutputStream); zipOutputStream.close(); return byteArrayOutputStreamForZippedPass.toByteArray(); }
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();//from w w w. ja v a 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:org.eclipse.cft.server.core.internal.CloudUtil.java
public static IStatus[] publishZip(List<IModuleResource> allResources, File tempFile, Set<IModuleResource> filterInFiles, IProgressMonitor monitor) { monitor = ProgressUtil.getMonitorFor(monitor); try {// w ww. jav a 2 s . c o m BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(tempFile)); ZipOutputStream zout = new ZipOutputStream(bout); addZipEntries(zout, allResources, filterInFiles); zout.close(); } catch (CoreException e) { return new IStatus[] { e.getStatus() }; } catch (Exception e) { return new Status[] { new Status(IStatus.ERROR, ServerPlugin.PLUGIN_ID, 0, NLS.bind(Messages.ERROR_CREATE_ZIP, tempFile.getName(), e.getLocalizedMessage()), e) }; } finally { if (tempFile != null && tempFile.exists()) tempFile.deleteOnExit(); } return EMPTY_STATUS; }
From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java
public static void zipDir(File sourceDir, File destFile) throws IOException { FileOutputStream fos = new FileOutputStream(destFile); ZipOutputStream zos = new ZipOutputStream(fos); zipDir(sourceDir, destFile, zos);//from www .j av a 2 s .c o m zos.close(); }
From source file:com.amalto.core.storage.hibernate.TypeMapping.java
private static Object _serializeValue(Object value, FieldMetadata sourceField, FieldMetadata targetField, Session session) {/*from w w w. jav a 2s .co m*/ if (targetField == null) { return value; } if (!targetField.isMany()) { Boolean targetZipped = targetField.<Boolean>getData(MetadataRepository.DATA_ZIPPED); Boolean sourceZipped = sourceField.<Boolean>getData(MetadataRepository.DATA_ZIPPED); if (sourceZipped == null && Boolean.TRUE.equals(targetZipped)) { try { ByteArrayOutputStream characters = new ByteArrayOutputStream(); OutputStream bos = new Base64OutputStream(characters); ZipOutputStream zos = new ZipOutputStream(bos); ZipEntry zipEntry = new ZipEntry("content"); //$NON-NLS-1$ zos.putNextEntry(zipEntry); zos.write(String.valueOf(value).getBytes("UTF-8")); //$NON-NLS-1$ zos.closeEntry(); zos.flush(); zos.close(); return new String(characters.toByteArray()); } catch (IOException e) { throw new RuntimeException("Unexpected compression exception", e); //$NON-NLS-1$ } } String targetSQLType = targetField.getType().getData(TypeMapping.SQL_TYPE); if (targetSQLType != null && SQL_TYPE_CLOB.equals(targetSQLType)) { if (value != null) { return Hibernate.getLobCreator(session).createClob(String.valueOf(value)); } else { return null; } } } return value; }