Examples of TarArchiveOutputStream


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

    }

    public void testFileEntryFromFile() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        TarArchiveOutputStream tos = null;
        TarArchiveInputStream tis = null;
        FileInputStream fis = null;
        try {
            archive = File.createTempFile("test.", ".tar", tmp[0]);
            archive.deleteOnExit();
            tos = new TarArchiveOutputStream(new FileOutputStream(archive));
            TarArchiveEntry in = new TarArchiveEntry(tmp[1], "foo");
            tos.putArchiveEntry(in);
            byte[] b = new byte[(int) tmp[1].length()];
            fis = new FileInputStream(tmp[1]);
            while (fis.read(b) > 0) {
                tos.write(b);
            }
            fis.close();
            fis = null;
            tos.closeArchiveEntry();
            tos.close();
            tos = null;
            tis = new TarArchiveInputStream(new FileInputStream(archive));
            TarArchiveEntry out = tis.getNextTarEntry();
            tis.close();
            tis = null;
            assertNotNull(out);
            assertEquals("foo", out.getName());
            assertEquals(tmp[1].length(), out.getSize());
            assertEquals(tmp[1].lastModified() / 1000,
                         out.getLastModifiedDate().getTime() / 1000);
            assertFalse(out.isDirectory());
        } finally {
            if (tis != null) {
                tis.close();
            }
            if (tos != null) {
                tos.close();
            }
            tryHardToDelete(archive);
            if (fis != null) {
                fis.close();
            }
View Full Code Here

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

        if ("ar".equalsIgnoreCase(archiverName)) {
            return new ArArchiveOutputStream(out);
        } else if ("zip".equalsIgnoreCase(archiverName)) {
            return new ZipArchiveOutputStream(out);
        } else if ("tar".equalsIgnoreCase(archiverName)) {
            return new TarArchiveOutputStream(out);
        } else if ("jar".equalsIgnoreCase(archiverName)) {
            return new JarArchiveOutputStream(out);
        } else if ("cpio".equalsIgnoreCase(archiverName)) {
            return new CpioArchiveOutputStream(out);
        }
View Full Code Here

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

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

   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

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

    }

    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

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

     * @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

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

            }
            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

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

     * @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

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

        // 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

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

    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
TOP
Copyright © 2018 www.massapi.com. 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.