Search in sources :

Example 1 with TarOutputStream

use of org.apache.tools.tar.TarOutputStream 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 TarOutputStream

use of org.apache.tools.tar.TarOutputStream in project gradle by gradle.

the class TarCopyAction method execute.

public WorkResult execute(final CopyActionProcessingStream stream) {
    final OutputStream outStr;
    try {
        outStr = compressor.createArchiveOutputStream(tarFile);
    } catch (Exception e) {
        throw new GradleException(String.format("Could not create TAR '%s'.", tarFile), e);
    }
    IoActions.withResource(outStr, new ErroringAction<OutputStream>() {

        @Override
        protected void doExecute(final OutputStream outStr) throws Exception {
            TarOutputStream tarOutStr;
            try {
                tarOutStr = new TarOutputStream(outStr);
            } catch (Exception e) {
                throw new GradleException(String.format("Could not create TAR '%s'.", tarFile), e);
            }
            tarOutStr.setLongFileMode(TarOutputStream.LONGFILE_GNU);
            stream.process(new StreamAction(tarOutStr));
            tarOutStr.close();
        }
    });
    return new SimpleWorkResult(true);
}
Also used : TarOutputStream(org.apache.tools.tar.TarOutputStream) OutputStream(java.io.OutputStream) TarOutputStream(org.apache.tools.tar.TarOutputStream) GradleException(org.gradle.api.GradleException) SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) CopyActionProcessingStreamAction(org.gradle.api.internal.file.CopyActionProcessingStreamAction) GradleException(org.gradle.api.GradleException)

Aggregations

OutputStream (java.io.OutputStream)2 TarOutputStream (org.apache.tools.tar.TarOutputStream)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 JarFile (java.util.jar.JarFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 TarEntry (org.apache.tools.tar.TarEntry)1 GradleException (org.gradle.api.GradleException)1 CopyActionProcessingStreamAction (org.gradle.api.internal.file.CopyActionProcessingStreamAction)1 SimpleWorkResult (org.gradle.api.internal.tasks.SimpleWorkResult)1 Test (org.junit.Test)1