Search in sources :

Example 1 with TarEntry

use of org.apache.tools.tar.TarEntry in project hadoop by apache.

the class TestFileUtil method testUnTar.

@Test(timeout = 30000)
public void testUnTar() throws IOException {
    setupDirs();
    // make a simple tar:
    final File simpleTar = new File(del, FILE);
    OutputStream os = new FileOutputStream(simpleTar);
    TarOutputStream tos = new TarOutputStream(os);
    try {
        TarEntry te = new TarEntry("/bar/foo");
        byte[] data = "some-content".getBytes("UTF-8");
        te.setSize(data.length);
        tos.putNextEntry(te);
        tos.write(data);
        tos.closeEntry();
        tos.flush();
        tos.finish();
    } finally {
        tos.close();
    }
    // successfully untar it into an existing dir:
    FileUtil.unTar(simpleTar, tmp);
    // check result:
    assertTrue(new File(tmp, "/bar/foo").exists());
    assertEquals(12, new File(tmp, "/bar/foo").length());
    final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog");
    regularFile.createNewFile();
    assertTrue(regularFile.exists());
    try {
        FileUtil.unTar(simpleTar, regularFile);
        assertTrue("An IOException expected.", false);
    } catch (IOException ioe) {
    // okay
    }
}
Also used : TarOutputStream(org.apache.tools.tar.TarOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TarOutputStream(org.apache.tools.tar.TarOutputStream) FileOutputStream(java.io.FileOutputStream) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 2 with TarEntry

use of org.apache.tools.tar.TarEntry in project OpenRefine by OpenRefine.

the class FileProjectManager method untar.

protected void untar(File destDir, InputStream inputStream) throws IOException {
    TarInputStream tin = new TarInputStream(inputStream);
    TarEntry tarEntry = null;
    while ((tarEntry = tin.getNextEntry()) != null) {
        File destEntry = new File(destDir, tarEntry.getName());
        File parent = destEntry.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        if (tarEntry.isDirectory()) {
            destEntry.mkdirs();
        } else {
            FileOutputStream fout = new FileOutputStream(destEntry);
            try {
                tin.copyEntryContents(fout);
            } finally {
                fout.close();
            }
        }
    }
    tin.close();
}
Also used : TarInputStream(org.apache.tools.tar.TarInputStream) FileOutputStream(java.io.FileOutputStream) TarEntry(org.apache.tools.tar.TarEntry) File(java.io.File)

Example 3 with TarEntry

use of org.apache.tools.tar.TarEntry in project OpenRefine by OpenRefine.

the class FileProjectManager method tarDir.

protected void tarDir(String relative, File dir, TarOutputStream tos) throws IOException {
    File[] files = dir.listFiles();
    for (File file : files) {
        if (!file.isHidden()) {
            String path = relative + file.getName();
            if (file.isDirectory()) {
                tarDir(path + File.separator, file, tos);
            } else {
                TarEntry entry = new TarEntry(path);
                entry.setMode(TarEntry.DEFAULT_FILE_MODE);
                entry.setSize(file.length());
                entry.setModTime(file.lastModified());
                tos.putNextEntry(entry);
                copyFile(file, tos);
                tos.closeEntry();
            }
        }
    }
}
Also used : TarEntry(org.apache.tools.tar.TarEntry) File(java.io.File)

Example 4 with TarEntry

use of org.apache.tools.tar.TarEntry in project OpenGrok by OpenGrok.

the class TarAnalyzer method analyze.

@Override
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
    ArrayList<String> names = new ArrayList<>();
    try (TarInputStream zis = new TarInputStream(src.getStream())) {
        TarEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String name = entry.getName();
            names.add(name);
            if (xrefOut != null) {
                Util.htmlize(name, xrefOut);
                xrefOut.append("<br/>");
            }
        }
    }
    doc.add(new TextField("full", new IteratorReader(names)));
}
Also used : IteratorReader(org.opensolaris.opengrok.analysis.IteratorReader) TarInputStream(org.apache.tools.tar.TarInputStream) ArrayList(java.util.ArrayList) TextField(org.apache.lucene.document.TextField) TarEntry(org.apache.tools.tar.TarEntry)

Example 5 with TarEntry

use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.

the class TarArchiver method visitSymlink.

@Override
public void visitSymlink(File link, String target, String relativePath) throws IOException {
    TarEntry e = new TarEntry(relativePath, LF_SYMLINK);
    try {
        StringBuffer linkName = (StringBuffer) LINKNAME_FIELD.get(e);
        linkName.setLength(0);
        linkName.append(target);
    } catch (IllegalAccessException x) {
        throw new IOException2("Failed to set linkName", x);
    }
    tar.putNextEntry(e);
    entriesWritten++;
}
Also used : TarEntry(org.apache.tools.tar.TarEntry) IOException2(hudson.util.IOException2)

Aggregations

TarEntry (org.apache.tools.tar.TarEntry)16 File (java.io.File)5 IOException (java.io.IOException)4 TarInputStream (org.apache.tools.tar.TarInputStream)3 IOException2 (hudson.util.IOException2)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 Matcher (java.util.regex.Matcher)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)1 TextField (org.apache.lucene.document.TextField)1 TarOutputStream (org.apache.tools.tar.TarOutputStream)1