Package org.apache.commons.compress.archivers.tar

Examples of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream


    byte[] bytes = new byte[len];
    r.nextBytes(bytes);

    File archiveFile = new File(p.toUri().getPath() + ".tar");
    archiveFile.createNewFile();
    TarArchiveOutputStream out = new TarArchiveOutputStream(
        new FileOutputStream(archiveFile));
    TarArchiveEntry entry = new TarArchiveEntry(p.getName());
    entry.setSize(bytes.length);
    out.putArchiveEntry(entry);
    out.write(bytes);
    out.closeArchiveEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(ConverterUtils.getYarnUrlFromPath(new Path(p.toString()
        + ".tar")));
    ret.setSize(len);
View Full Code Here


   public static File tar(File baseDir, File tarFile) throws IOException {
      // Check that the directory is a directory, and get its contents
      checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir);
      File[] files = baseDir.listFiles();
      String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath()));
      TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
      tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
      try {
         for (File file : files) {
            TarArchiveEntry tarEntry = new TarArchiveEntry(file);
            tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString())));
            tos.putArchiveEntry(tarEntry);
            if (!file.isDirectory()) {
               Files.asByteSource(file).copyTo(tos);
            }
            tos.closeArchiveEntry();
         }
      } finally {
         tos.close();
      }
      return tarFile;
   }
View Full Code Here

    }

    public static void write() throws Throwable {
        FileOutputStream fos = new FileOutputStream("C:\\X2.tgz");
        GZIPOutputStream out = new GZIPOutputStream(fos);
        TarArchiveOutputStream tos = new TarArchiveOutputStream(out);
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // byte[] buf = Streams.readBytes(new FileInputStream("C:\\wendal.ts"));
        for (int i = 0; i < 2; i++) {
            try {
                ArchiveEntry entry = tos.createArchiveEntry(new File("C:\\Q.zip"),
                                                            Strings.dup('A', 500) + i);
                tos.putArchiveEntry(entry);
                // tos.write(buf);
            tos.closeArchiveEntry();
            }
            catch (Exception e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
            }
        }
        tos.flush();
        tos.finish();
        tos.close();
    }
View Full Code Here

     * @throws LDPathException
     */
    public ArchiveInputStream createSchemaArchive(String coreName, String ldPathProgram) throws LDPathException {
        ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
        ArchiveStreamFactory asf = new ArchiveStreamFactory();
        TarArchiveOutputStream tarOutputStream = null;
        try {
            tarOutputStream = (TarArchiveOutputStream) asf.createArchiveOutputStream("tar", out);
        } catch (ArchiveException e) {
            String msg = "Cannot create an empty tar archive";
            logger.error(msg, e);
            throw new LDPathException(msg, e);
        }

        try {
            InputStream is = getSolrTemplateStream();
            ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
            ZipArchiveEntry ze = null;
            byte[] schemaFile = null;
            while ((ze = zis.getNextZipEntry()) != null) {
                if (SOLR_TEMPLATE_SCHEMA.equals(ze.getName())) {
                    schemaFile = createSchemaXML(getLDPathProgram(ldPathProgram), IOUtils.toByteArray(zis));
                    TarArchiveEntry te = new TarArchiveEntry(coreName + SOLR_SCHEMA);
                    te.setSize(schemaFile.length);
                    tarOutputStream.putArchiveEntry(te);
                    tarOutputStream.write(schemaFile);
                    tarOutputStream.closeArchiveEntry();
                } else {
                    TarArchiveEntry te = new TarArchiveEntry(ze.getName().replaceAll(SOLR_TEMPLATE_NAME,
                        coreName));
                    te.setSize(ze.getSize());
                    tarOutputStream.putArchiveEntry(te);
                    tarOutputStream.write(IOUtils.toByteArray(zis));
                    tarOutputStream.closeArchiveEntry();
                }

            }
            if (schemaFile == null) {
                throw new LDPathException("Schema template ZIP should include: " + SOLR_TEMPLATE_SCHEMA);
            }
            tarOutputStream.finish();
            tarOutputStream.close();
        } catch (IOException e) {
            logger.error("", e);
            throw new LDPathException(e);
        }

View Full Code Here

            }
            return zip;
        }
        if (TAR.equalsIgnoreCase(archiverName)) {
            if (entryEncoding != null) {
                return new TarArchiveOutputStream(out, entryEncoding);
            } else {
                return new TarArchiveOutputStream(out);
            }
        }
        if (JAR.equalsIgnoreCase(archiverName)) {
            return new JarArchiveOutputStream(out);
        }
View Full Code Here

     * @throws LDPathException
     */
    public ArchiveInputStream createSchemaArchive(String coreName, String ldPathProgram) throws LDPathException {
        ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
        ArchiveStreamFactory asf = new ArchiveStreamFactory();
        TarArchiveOutputStream tarOutputStream = null;
        try {
            tarOutputStream = (TarArchiveOutputStream) asf.createArchiveOutputStream("tar", out);
        } catch (ArchiveException e) {
            String msg = "Cannot create an empty tar archive";
            logger.error(msg, e);
            throw new LDPathException(msg, e);
        }

        try {
            InputStream is = getSolrTemplateStream();
            ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
            ZipArchiveEntry ze = null;
            byte[] schemaFile = null;
            while ((ze = zis.getNextZipEntry()) != null) {
                if (SOLR_TEMPLATE_SCHEMA.equals(ze.getName())) {
                    schemaFile = createSchemaXML(getLDPathProgram(ldPathProgram), IOUtils.toByteArray(zis));
                    TarArchiveEntry te = new TarArchiveEntry(coreName + SOLR_SCHEMA);
                    te.setSize(schemaFile.length);
                    tarOutputStream.putArchiveEntry(te);
                    tarOutputStream.write(schemaFile);
                    tarOutputStream.closeArchiveEntry();
                } else {
                    TarArchiveEntry te = new TarArchiveEntry(ze.getName().replaceAll(SOLR_TEMPLATE_NAME,
                        coreName));
                    te.setSize(ze.getSize());
                    tarOutputStream.putArchiveEntry(te);
                    tarOutputStream.write(IOUtils.toByteArray(zis));
                    tarOutputStream.closeArchiveEntry();
                }

            }
            if (schemaFile == null) {
                throw new LDPathException("Schema template ZIP should include: " + SOLR_TEMPLATE_SCHEMA);
            }
            tarOutputStream.finish();
            tarOutputStream.close();
        } catch (IOException e) {
            logger.error("", e);
            throw new LDPathException(e);
        }

View Full Code Here

        // unix: download a tar.gz file with pt.py set with execute permissions
        response.setHeader("Content-Disposition", "attachment; filename=\"pt.tar.gz\"");

        OutputStream os = response.getOutputStream();
        CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, os);
        TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
        tos.setAddPaxHeadersForNonAsciiNames(true);
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

        // add the Python script
        TarArchiveEntry pyEntry = new TarArchiveEntry("pt");
        pyEntry.setMode(FileMode.EXECUTABLE_FILE.getBits());
        pyEntry.setModTime(lastModified);
        pyEntry.setSize(pyBytes.length);
        tos.putArchiveEntry(pyEntry);
        tos.write(pyBytes);
        tos.closeArchiveEntry();

        // add a brief readme
        byte [] txtBytes = readAll(getClass().getResourceAsStream("/pt.txt"));
        TarArchiveEntry txtEntry = new TarArchiveEntry("README");
        txtEntry.setMode(FileMode.REGULAR_FILE.getBits());
        txtEntry.setModTime(lastModified);
        txtEntry.setSize(txtBytes.length);
        tos.putArchiveEntry(txtEntry);
        tos.write(txtBytes);
        tos.closeArchiveEntry();

        // cleanup
        tos.finish();
        tos.close();
        cos.close();
        os.flush();
      }
    } catch (Exception e) {
      e.printStackTrace();
View Full Code Here

    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
      tw.reset();
      tw.addTree(commit.getTree());
      TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
      tos.setAddPaxHeadersForNonAsciiNames(true);
      tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
      if (!StringUtils.isEmpty(basePath)) {
        PathFilter f = PathFilter.create(basePath);
        tw.setFilter(f);
      }
      tw.setRecursive(true);
      MutableObjectId id = new MutableObjectId();
      long modified = commit.getAuthorIdent().getWhen().getTime();
      while (tw.next()) {
        FileMode mode = tw.getFileMode(0);
        if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
          continue;
        }
        tw.getObjectId(id, 0);

        ObjectLoader loader = repository.open(id);
        if (FileMode.SYMLINK == mode) {
          TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(),TarArchiveEntry.LF_SYMLINK);
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          loader.copyTo(bos);
          entry.setLinkName(bos.toString());
          entry.setModTime(modified);
          tos.putArchiveEntry(entry);
          tos.closeArchiveEntry();
        } else {
          TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
          entry.setMode(mode.getBits());
          entry.setModTime(modified);
          entry.setSize(loader.getSize());
          tos.putArchiveEntry(entry);
          loader.copyTo(tos);
          tos.closeArchiveEntry();
        }
      }
      tos.finish();
      tos.close();
      cos.close();
      success = true;
    } catch (IOException e) {
      error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
    } finally {
View Full Code Here

    }

    @Override
    protected OutputStream stream(ByteArrayOutputStream baos)
    {
            return new TarArchiveOutputStream(baos);
   
View Full Code Here

    byte[] bytes = new byte[len];
    r.nextBytes(bytes);

    File archiveFile = new File(p.toUri().getPath() + ".tar");
    archiveFile.createNewFile();
    TarArchiveOutputStream out = new TarArchiveOutputStream(
        new FileOutputStream(archiveFile));
    TarArchiveEntry entry = new TarArchiveEntry(p.getName());
    entry.setSize(bytes.length);
    out.putArchiveEntry(entry);
    out.write(bytes);
    out.closeArchiveEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(ConverterUtils.getYarnUrlFromPath(new Path(p.toString()
        + ".tar")));
    ret.setSize(len);
View Full Code Here

TOP

Related Classes of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.