List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry TarArchiveEntry
public TarArchiveEntry(byte[] headerBuf)
From source file:com.st.maven.debian.DebianPackageMojo.java
private static void writeDirectory(TarArchiveOutputStream tar, String dirName) throws MojoExecutionException { try {/*from w w w . j a v a2 s . c o m*/ if (!dirName.endsWith("/")) { dirName = dirName + "/"; } TarArchiveEntry curEntry = new TarArchiveEntry(dirName); tar.putArchiveEntry(curEntry); tar.closeArchiveEntry(); } catch (Exception e) { throw new MojoExecutionException("unable to add directory: " + dirName, e); } }
From source file:io.anserini.index.IndexUtils.java
public void dumpDocumentVectors(String reqDocidsPath, DocVectorWeight weight) throws IOException { String outFileName = weight == null ? reqDocidsPath + ".docvector.tar.gz" : reqDocidsPath + ".docvector." + weight + ".tar.gz"; LOG.info("Start dump document vectors with weight " + weight); InputStream in = getReadFileStream(reqDocidsPath); BufferedReader bRdr = new BufferedReader(new InputStreamReader(in)); FileOutputStream fOut = new FileOutputStream(new File(outFileName)); BufferedOutputStream bOut = new BufferedOutputStream(fOut); GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(bOut); TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut); Map<Term, Integer> docFreqMap = new HashMap<>(); int numNonEmptyDocs = reader.getDocCount(LuceneDocumentGenerator.FIELD_BODY); String docid;// w w w . j a v a 2s . co m int counter = 0; while ((docid = bRdr.readLine()) != null) { counter++; // get term frequency Terms terms = reader.getTermVector(convertDocidToLuceneDocid(docid), LuceneDocumentGenerator.FIELD_BODY); if (terms == null) { // We do not throw exception here because there are some // collections in which part of documents don't have document vectors LOG.warn("Document vector not stored for doc " + docid); continue; } TermsEnum te = terms.iterator(); if (te == null) { LOG.warn("Document vector not stored for doc " + docid); continue; } Term term; long freq; // iterate every term and write and store in Map Map<String, String> docVectors = new HashMap<>(); while ((te.next()) != null) { term = new Term(LuceneDocumentGenerator.FIELD_BODY, te.term()); freq = te.totalTermFreq(); switch (weight) { case NONE: docVectors.put(term.bytes().utf8ToString(), String.valueOf(freq)); break; case TF_IDF: int docFreq; if (docFreqMap.containsKey(term)) { docFreq = docFreqMap.get(term); } else { try { docFreq = reader.docFreq(term); } catch (Exception e) { LOG.error("Cannot find term " + term.toString() + " in indexing file."); continue; } docFreqMap.put(term, docFreq); } float tfIdf = (float) (freq * Math.log(numNonEmptyDocs * 1.0 / docFreq)); docVectors.put(term.bytes().utf8ToString(), String.format("%.6f", tfIdf)); break; } } // Count size and write byte[] bytesOut = docVectors.entrySet().stream().map(e -> e.getKey() + " " + e.getValue()) .collect(joining("\n")).getBytes(StandardCharsets.UTF_8); TarArchiveEntry tarEntry = new TarArchiveEntry(new File(docid)); tarEntry.setSize(bytesOut.length + String.format("<DOCNO>%s</DOCNO>\n", docid).length()); tOut.putArchiveEntry(tarEntry); tOut.write(String.format("<DOCNO>%s</DOCNO>\n", docid).getBytes()); tOut.write(bytesOut); tOut.closeArchiveEntry(); if (counter % 100000 == 0) { LOG.info(counter + " files have been dumped."); } } tOut.close(); LOG.info("Document Vectors are output to: " + outFileName); }
From source file:com.st.maven.debian.DebianPackageMojo.java
private void fillControlTar(Config config, ArFileOutputStream output) throws MojoExecutionException { TarArchiveOutputStream tar = null;/*from ww w. j a va 2 s . co m*/ try { tar = new TarArchiveOutputStream(new GZIPOutputStream(new ArWrapper(output))); tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveEntry rootDir = new TarArchiveEntry("./"); tar.putArchiveEntry(rootDir); tar.closeArchiveEntry(); byte[] controlData = processTemplate(freemarkerConfig, config, "control.ftl"); TarArchiveEntry controlEntry = new TarArchiveEntry("./control"); controlEntry.setSize(controlData.length); tar.putArchiveEntry(controlEntry); tar.write(controlData); tar.closeArchiveEntry(); byte[] preinstBaseData = processTemplate("preinst", freemarkerConfig, config, combine("preinst.ftl", BASE_DIR + File.separator + "preinst", false)); long size = preinstBaseData.length; TarArchiveEntry preinstEntry = new TarArchiveEntry("./preinst"); preinstEntry.setSize(size); preinstEntry.setMode(0755); tar.putArchiveEntry(preinstEntry); tar.write(preinstBaseData); tar.closeArchiveEntry(); byte[] postinstBaseData = processTemplate("postinst", freemarkerConfig, config, combine("postinst.ftl", BASE_DIR + File.separator + "postinst", true)); size = postinstBaseData.length; TarArchiveEntry postinstEntry = new TarArchiveEntry("./postinst"); postinstEntry.setSize(size); postinstEntry.setMode(0755); tar.putArchiveEntry(postinstEntry); tar.write(postinstBaseData); tar.closeArchiveEntry(); byte[] prermBaseData = processTemplate("prerm", freemarkerConfig, config, combine("prerm.ftl", BASE_DIR + File.separator + "prerm", false)); size = prermBaseData.length; TarArchiveEntry prermEntry = new TarArchiveEntry("./prerm"); prermEntry.setSize(size); prermEntry.setMode(0755); tar.putArchiveEntry(prermEntry); tar.write(prermBaseData); tar.closeArchiveEntry(); byte[] postrmBaseData = processTemplate("postrm", freemarkerConfig, config, combine("postrm.ftl", BASE_DIR + File.separator + "postrm", false)); size = postrmBaseData.length; TarArchiveEntry postrmEntry = new TarArchiveEntry("./postrm"); postrmEntry.setSize(size); postrmEntry.setMode(0755); tar.putArchiveEntry(postrmEntry); tar.write(postrmBaseData); tar.closeArchiveEntry(); } catch (Exception e) { throw new MojoExecutionException("unable to create control tar", e); } finally { if (tar != null) { try { tar.close(); } catch (IOException e) { getLog().error("unable to finish tar", e); } } } }
From source file:gov.nih.nci.ncicb.tcga.dcc.dam.processors.FilePackagerFastTest.java
@Test public void testBigFileMode() throws IOException { final File tempTarFile = new File(THIS_FOLDER + "/fakeBigTar.tar.gz"); try {// w w w. ja v a2 s . c om TarArchiveOutputStream tarArchiveOutputStream = packager.makeTarGzOutputStream(tempTarFile); TarArchiveEntry fakeEntry = new TarArchiveEntry("test"); fakeEntry.setSize(9999999999L); tarArchiveOutputStream.putArchiveEntry(fakeEntry); // if the TarArchiveOutputStream cannot handle entries of this size, it will fail. // so if it doesn't then the test passes. I can't think of anything to assert... } finally { // delete temp tar file made during test tempTarFile.deleteOnExit(); } }
From source file:fr.ortolang.diffusion.api.content.ContentResource.java
private ResponseBuilder handleExport(boolean followSymlink, String filename, String format, final List<String> paths, final Pattern pattern) throws UnsupportedEncodingException { ResponseBuilder builder;//from w ww.j a v a 2 s .c o m switch (format) { case "zip": { LOGGER.log(Level.FINE, "exporting using format zip"); builder = Response.ok(); builder.header("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(filename, "utf-8") + ".zip"); builder.type("application/zip"); StreamingOutput stream = output -> { try (ZipArchiveOutputStream out = new ZipArchiveOutputStream(output)) { for (String path : paths) { try { String key = resolveContentPath(path); ArchiveEntryFactory factory = (name, time, size) -> { ZipArchiveEntry entry = new ZipArchiveEntry(name); if (time != -1) { entry.setTime(time); } if (size != -1) { entry.setSize(size); } return entry; }; exportToArchive(key, out, factory, PathBuilder.fromPath(path), false, pattern); } catch (AccessDeniedException e) { LOGGER.log(Level.FINEST, "access denied during export to zip", e); } catch (BrowserServiceException | CoreServiceException | AliasNotFoundException | KeyNotFoundException | OrtolangException e) { LOGGER.log(Level.INFO, "unable to export path to zip", e); } catch (InvalidPathException e) { LOGGER.log(Level.FINEST, "invalid path during export to zip", e); } catch (PathNotFoundException e) { LOGGER.log(Level.FINEST, "path not found during export to zip", e); } catch (ExportToArchiveIOException e) { LOGGER.log(Level.SEVERE, "unexpected IO error during export to archive, stopping export", e); break; } } } }; builder.entity(stream); break; } case "tar": { LOGGER.log(Level.FINE, "exporting using format tar"); builder = Response.ok(); builder.header("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(filename, "utf-8") + ".tar.gz"); builder.type("application/x-gzip"); StreamingOutput stream = output -> { try (GzipCompressorOutputStream gout = new GzipCompressorOutputStream(output); TarArchiveOutputStream out = new TarArchiveOutputStream(gout)) { for (String path : paths) { try { String key = resolveContentPath(path); ArchiveEntryFactory factory = (name, time, size) -> { TarArchiveEntry entry = new TarArchiveEntry(name); if (time != -1) { entry.setModTime(time); } if (size != -1) { entry.setSize(size); } return entry; }; exportToArchive(key, out, factory, PathBuilder.fromPath(path), false, pattern); } catch (BrowserServiceException | CoreServiceException | AliasNotFoundException | KeyNotFoundException | OrtolangException e) { LOGGER.log(Level.INFO, "unable to export path to tar", e); } catch (InvalidPathException e) { LOGGER.log(Level.FINEST, "invalid path during export to tar", e); } catch (PathNotFoundException e) { LOGGER.log(Level.FINEST, "path not found during export to tar", e); } catch (ExportToArchiveIOException e) { LOGGER.log(Level.SEVERE, "unexpected IO error during export to archive, stopping export", e); break; } } } }; builder.entity(stream); break; } default: builder = Response.status(Status.BAD_REQUEST).entity("export format [" + format + "] is not supported"); } return builder; }
From source file:io.anserini.index.IndexUtils.java
public void dumpRawDocuments(String reqDocidsPath, boolean prependDocid) throws IOException, NotStoredException { LOG.info("Start dump raw documents" + (prependDocid ? " with Docid prepended" : ".")); InputStream in = getReadFileStream(reqDocidsPath); BufferedReader bRdr = new BufferedReader(new InputStreamReader(in)); FileOutputStream fOut = new FileOutputStream(new File(reqDocidsPath + ".output.tar.gz")); BufferedOutputStream bOut = new BufferedOutputStream(fOut); GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(bOut); TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut); String docid;//from www . j a v a 2 s . com int counter = 0; while ((docid = bRdr.readLine()) != null) { counter += 1; Document d = reader.document(convertDocidToLuceneDocid(docid)); IndexableField doc = d.getField(LuceneDocumentGenerator.FIELD_RAW); if (doc == null) { throw new NotStoredException("Raw documents not stored!"); } TarArchiveEntry tarEntry = new TarArchiveEntry(new File(docid)); byte[] bytesOut = doc.stringValue().getBytes(StandardCharsets.UTF_8); tarEntry.setSize( bytesOut.length + (prependDocid ? String.format("<DOCNO>%s</DOCNO>\n", docid).length() : 0)); tOut.putArchiveEntry(tarEntry); if (prependDocid) { tOut.write(String.format("<DOCNO>%s</DOCNO>\n", docid).getBytes()); } tOut.write(bytesOut); tOut.closeArchiveEntry(); if (counter % 100000 == 0) { LOG.info(counter + " files have been dumped."); } } tOut.close(); LOG.info(String.format("Raw documents are output to: %s", reqDocidsPath + ".output.tar.gz")); }
From source file:ezbake.protect.ezca.EzCABootstrap.java
protected static void addTarArchiveEntry(final TarArchiveOutputStream tao, String name, byte[] data) { TarArchiveEntry tae = new TarArchiveEntry(name); try {/*from ww w . j a va2s.c o m*/ tae.setSize(data.length); tao.putArchiveEntry(tae); tao.write(data); tao.closeArchiveEntry(); } catch (IOException e) { e.printStackTrace(); } }
From source file:edu.mit.lib.bagit.Filler.java
private void fillArchive(File dirFile, String relBase, ArchiveOutputStream out) throws IOException { for (File file : dirFile.listFiles()) { String relPath = relBase + File.separator + file.getName(); if (file.isDirectory()) { fillArchive(file, relPath, out); } else {//from w ww . j av a 2 s.c o m TarArchiveEntry entry = new TarArchiveEntry(relPath); entry.setSize(file.length()); entry.setModTime(0L); out.putArchiveEntry(entry); Files.copy(file.toPath(), out); out.closeArchiveEntry(); } } }
From source file:lucee.commons.io.compress.CompressUtil.java
private static void compressTar(String parent, Resource source, TarArchiveOutputStream tos, int mode) throws IOException { if (source.isFile()) { //TarEntry entry = (source instanceof FileResource)?new TarEntry((FileResource)source):new TarEntry(parent); TarArchiveEntry entry = new TarArchiveEntry(parent); entry.setName(parent);//from www . j av a2 s. c o m // mode //100777 TODO ist das so ok? if (mode > 0) entry.setMode(mode); else if ((mode = source.getMode()) > 0) entry.setMode(mode); entry.setSize(source.length()); entry.setModTime(source.lastModified()); tos.putArchiveEntry(entry); try { IOUtil.copy(source, tos, false); } finally { tos.closeArchiveEntry(); } } else if (source.isDirectory()) { compressTar(parent, source.listResources(), tos, mode); } }
From source file:bb.io.TarUtil.java
/** * Writes path as a new TarArchiveEntry to tarArchiveOutputStream. * If path is a normal file, then next writes path's data to tarArchiveOutputStream. * <p>/* w w w .j a v a 2s. c o m*/ * If path is a directory, then this method additionally calls itself on the contents * (thus recursing thru the entire directory tree). * <p> * <b>Warning</b>: several popular programs (e.g. winzip) fail to display mere directory entries. * Furthermore, if just a directory entry is present (i.e. it is empty), they also may fail * to create a new empty directoy when extracting the TAR file's contents. * These are bugs in their behavior. * <p> * An optional FileFilter can be supplied to screen out paths that would otherwise be archived. * <p> * <i>This method does not close tarArchiveOutputStream:</i> that is the responsibility of the caller. * <p> * The caller also must take on the responsibility to not do anything stupid, like write * path more than once, or have the path be the same File that tarArchiveOutputStream is writing to. * <p> * @param path the File to archive * @param fileParent the FileParent for path * @param tarArchiveOutputStream the TarArchiveOutputStream that will write the archive data to * @param filter a FileFilter that can use to screen out certain files from being written to the archive; may be null (so everything specified by path gets archived) * @throws Exception if any Throwable is caught; the Throwable is stored as the cause, and the message stores path's information; * here are some of the possible causes: * <ol> * <li>IllegalArgumentException if path fails {@link #isTarable isTarable}</li> * <li>SecurityException if a security manager exists and its SecurityManager.checkRead method denies read access to path</li> * <li>IOException if an I/O problem occurs</li> * </ol> */ private static void archive(File path, FileParent fileParent, TarArchiveOutputStream tarArchiveOutputStream, FileFilter filter) throws Exception { try { if ((filter != null) && !filter.accept(path)) return; if (!isTarable(path)) throw new IllegalArgumentException("path = " + path.getPath() + " failed isTarable"); if (giveUserFeedback) ConsoleUtil.overwriteLine("TarUtil.archive: " + path.getPath()); TarArchiveEntry entry = new TarArchiveEntry(path); entry.setName(fileParent.getRelativePath(path, '/')); // Note: getRelativePath will ensure that '/' is the path separator and that directories end with '/' tarArchiveOutputStream.putNextEntry(entry); if (path.isFile()) readInFile(path, tarArchiveOutputStream); tarArchiveOutputStream.closeEntry(); if (path.isDirectory()) { for (File fileChild : DirUtil.getContents(path, null)) { // supply null, since we test at beginning of this method (supplying filter here will just add a redundant test) archive(fileChild, fileParent, tarArchiveOutputStream, filter); } } } catch (Throwable t) { String preface = "See cause for the underlying Throwable; happened for path = "; if ((t instanceof Exception) && (t.getMessage().startsWith(preface))) throw (Exception) t; // CRITICAL: see if t was actually the wrapping Exception generated by a subsequent recursive call to this method, and if so simply rethrow it unmodified to avoid excessive exception chaining throw new Exception(preface + (path != null ? path.getPath() : "<null>"), t); } }