List of usage examples for java.util.zip ZipOutputStream closeEntry
public void closeEntry() throws IOException
From source file:com.datasalt.pangool.solr.SolrRecordWriter.java
/** * Write a file to a zip output stream, removing leading path name components from the actual file name when creating * the zip file entry.//from w ww . j av a2 s.com * * The entry placed in the zip file is <code>baseName</code>/ <code>relativePath</code>, where * <code>relativePath</code> is constructed by removing a leading <code>root</code> from the path for * <code>itemToZip</code>. * * If <code>itemToZip</code> is an empty directory, it is ignored. If <code>itemToZip</code> is a directory, the * contents of the directory are added recursively. * * @param zos * The zip output stream * @param baseName * The base name to use for the file name entry in the zip file * @param root * The path to remove from <code>itemToZip</code> to make a relative path name * @param itemToZip * The path to the file to be added to the zip file * @return the number of entries added * @throws IOException */ static public int zipDirectory(final Configuration conf, final ZipOutputStream zos, final String baseName, final String root, final Path itemToZip) throws IOException { LOG.info(String.format("zipDirectory: %s %s %s", baseName, root, itemToZip)); LocalFileSystem localFs = FileSystem.getLocal(conf); int count = 0; final FileStatus itemStatus = localFs.getFileStatus(itemToZip); if (itemStatus.isDir()) { final FileStatus[] statai = localFs.listStatus(itemToZip); // Add a directory entry to the zip file final String zipDirName = relativePathForZipEntry(itemToZip.toUri().getPath(), baseName, root); final ZipEntry dirZipEntry = new ZipEntry(zipDirName + Path.SEPARATOR_CHAR); LOG.info(String.format("Adding directory %s to zip", zipDirName)); zos.putNextEntry(dirZipEntry); zos.closeEntry(); count++; if (statai == null || statai.length == 0) { LOG.info(String.format("Skipping empty directory %s", itemToZip)); return count; } for (FileStatus status : statai) { count += zipDirectory(conf, zos, baseName, root, status.getPath()); } LOG.info(String.format("Wrote %d entries for directory %s", count, itemToZip)); return count; } final String inZipPath = relativePathForZipEntry(itemToZip.toUri().getPath(), baseName, root); if (inZipPath.length() == 0) { LOG.warn(String.format("Skipping empty zip file path for %s (%s %s)", itemToZip, root, baseName)); return 0; } // Take empty files in case the place holder is needed FSDataInputStream in = null; try { in = localFs.open(itemToZip); final ZipEntry ze = new ZipEntry(inZipPath); ze.setTime(itemStatus.getModificationTime()); // Comments confuse looking at the zip file // ze.setComment(itemToZip.toString()); zos.putNextEntry(ze); IOUtils.copyBytes(in, zos, conf, false); zos.closeEntry(); LOG.info(String.format("Wrote %d entries for file %s", count, itemToZip)); return 1; } finally { in.close(); } }
From source file:io.apigee.buildTools.enterprise4g.utils.ZipUtils.java
static void addDir(File dirObj, ZipOutputStream out, File root, String prefix) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { addDir(files[i], out, root, prefix); continue; }/*from ww w. j a v a2 s . co m*/ FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); log.debug(" Adding: " + files[i].getAbsolutePath()); String relativePath = files[i].getCanonicalPath().substring(root.getCanonicalPath().length()); while (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } while (relativePath.startsWith("\\")) { relativePath = relativePath.substring(1); } String left = Matcher.quoteReplacement("\\"); String right = Matcher.quoteReplacement("/"); relativePath = relativePath.replaceAll(left, right); relativePath = (prefix == null) ? relativePath : prefix + "/" + relativePath; out.putNextEntry(new ZipEntry(relativePath)); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.close(); } }
From source file:com.google.mr4c.sources.LogsDatasetSource.java
private void addZipEntry(ZipOutputStream zipStream, String name) throws IOException { ZipEntry entry = new ZipEntry(name); zipStream.putNextEntry(entry);//from w w w . j a v a 2 s . c o m DataFileSource entrySource = m_fileSrc.getFileSource(name); InputStream input = entrySource.getFileInputStream(); try { IOUtils.copy(input, zipStream); zipStream.closeEntry(); } finally { input.close(); } }
From source file:com.solidmaps.webapp.service.ApostilamentosServiceImpl.java
private FileSystemResource generateZip(CompanyEntity companyEntity) { String fileName = FILE_PATH + "Apostilamento-" + companyEntity.getCnpj() + ".zip"; FileOutputStream fos = null;/*w ww. j av a 2s . c om*/ try { fos = new FileOutputStream(fileName); ZipOutputStream zos = new ZipOutputStream(fos); this.addToZip(zos, fileFederal); this.addToZip(zos, fileCivil); this.addToZip(zos, fileExercito); zos.closeEntry(); zos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } FileSystemResource fileSystemResource = new FileSystemResource(fileName); return fileSystemResource; }
From source file:org.codice.ddf.configuration.migration.MigrationZipFileTest.java
private void createZipWithUnencryptedContents(Path zipPath, Path keyPath, Path checksumPath) throws Exception { createEncryptionCipher(keyPath);//w w w. j a v a 2s .c o m ZipEntry zipEntry = new ZipEntry("foo"); ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile())); zipOutputStream.putNextEntry(zipEntry); IOUtils.write("bar", zipOutputStream, StandardCharsets.UTF_8); zipOutputStream.closeEntry(); zipOutputStream.close(); createChecksum(zipPath, checksumPath); }
From source file:eu.europa.ejusticeportal.dss.controller.action.DownloadSignedPdf.java
/** * @param sf/*www . j a va 2 s .c om*/ * @param response * @throws IOException */ private void writeZip(SignedForm sf, HttpServletResponse response, String fileName) throws IOException { ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); ZipEntry ze = new ZipEntry(fileName); zos.putNextEntry(ze); zos.write(sf.getDocument()); ze = new ZipEntry(fileName + ".xml"); zos.putNextEntry(ze); zos.write(sf.getDetachedSignature()); zos.closeEntry(); zos.close(); }
From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java
public Message toMessage(Object object, MessageProperties props) throws MessageConversionException { if (object instanceof BatchMessage) { BatchMessage batch = (BatchMessage) object; props.setCorrelationId(batch.getId().getBytes()); props.setContentType("application/zip"); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ZipOutputStream zout = new ZipOutputStream(bout); for (Map.Entry<String, String> msg : batch.getMessages().entrySet()) { ZipEntry zentry = new ZipEntry(msg.getKey()); try { zout.putNextEntry(zentry); zout.write(msg.getValue().getBytes()); zout.closeEntry(); } catch (IOException e) { throw new MessageConversionException(e.getMessage(), e); }// w w w. j a v a2s. c o m } try { zout.flush(); zout.close(); } catch (IOException e) { throw new MessageConversionException(e.getMessage(), e); } return new Message(bout.toByteArray(), props); } else { throw new MessageConversionException( "Cannot convert object " + String.valueOf(object) + " using " + getClass().toString()); } }
From source file:de.baumann.thema.RequestActivity.java
private static void zipFile(final String path, final ZipOutputStream out, final String relPath) throws IOException { final File file = new File(path); if (!file.exists()) { if (DEBUG) Log.d(TAG, file.getName() + " does NOT exist!"); return;// ww w .j a va 2 s .c o m } final byte[] buf = new byte[1024]; final String[] files = file.list(); if (file.isFile()) { try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) { out.putNextEntry(new ZipEntry(relPath + file.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } catch (ZipException zipE) { if (DEBUG) Log.d(TAG, zipE.getMessage()); } finally { if (out != null) out.closeEntry(); } } else if (files.length > 0) // non-empty folder { for (String file1 : files) { zipFile(path + "/" + file1, out, relPath + file.getName() + "/"); } } }
From source file:com.coinblesk.client.backup.BackupDialogFragment.java
private void addZipEntry(String filename, byte[] data, ZipOutputStream zos) throws IOException { ZipEntry entry = new ZipEntry(filename); zos.putNextEntry(entry);/*from ww w . j a v a 2s .co m*/ zos.write(data); zos.closeEntry(); zos.flush(); }
From source file:com.replaymod.sponge.recording.AbstractRecorder.java
@Override public void endRecording(OutputStream out, ReplayMetaData metaData) throws IllegalStateException, IOException { if (metaData == null) { Preconditions.checkState(rawOutputs.remove(out), "Specified output is unknown or meta data is missing."); out.flush();//w ww.j a v a2 s . c om out.close(); } else { ZipOutputStream zipOut = outputs.remove(out); if (zipOut == null) { throw new IllegalStateException("Specified output is unknown or contains raw data."); } zipOut.closeEntry(); zipOut.putNextEntry(new ZipEntry("metaData.json")); zipOut.write(toJson(metaData).getBytes()); zipOut.closeEntry(); zipOut.flush(); zipOut.close(); } }