Search in sources :

Example 6 with TarInputStream

use of org.apache.tools.tar.TarInputStream in project jdeb by tcurdt.

the class DebAntTaskTestCase method testBZip2Compression.

@Test
public void testBZip2Compression() throws Exception {
    project.executeTarget("bzip2-compression");
    File deb = new File("target/test-classes/test.deb");
    assertTrue("package not build", deb.exists());
    final AtomicBoolean found = new AtomicBoolean(false);
    ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(deb));
    ArchiveWalker.walk(in, new ArchiveVisitor<ArArchiveEntry>() {

        public void visit(ArArchiveEntry entry, byte[] content) throws IOException {
            if (entry.getName().equals("data.tar.bz2")) {
                found.set(true);
                assertEquals("header 0", (byte) 'B', content[0]);
                assertEquals("header 1", (byte) 'Z', content[1]);
                TarInputStream tar = new TarInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream(content)));
                while ((tar.getNextEntry()) != null) ;
                tar.close();
            }
        }
    });
    assertTrue("bz2 file not found", found.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArArchiveEntry(org.apache.commons.compress.archivers.ar.ArArchiveEntry) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarInputStream(org.apache.tools.tar.TarInputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 7 with TarInputStream

use of org.apache.tools.tar.TarInputStream in project warn-report by saaavsaaa.

the class ZipUtil method unTarGz.

// ------------------------------------------------------------------------------------------------------
/**
 * .tar.gz文件可以看做先用tar打包,再使用gz进行压缩
 * @param file 要解压的tar.gz文件对象
 * @param outputDir 要解压到某个指定的目录下
 * @throws IOException
 */
public static void unTarGz(File file, String outputDir) throws IOException {
    TarInputStream tarIn = null;
    try {
        tarIn = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))), 1024 * 2);
        // 创建输出目录
        createDirectory(outputDir, null);
        TarEntry entry = null;
        while ((entry = tarIn.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                // 是目录
                entry.getName();
                // 创建空目录
                createDirectory(outputDir, entry.getName());
            } else {
                // 是文件
                File tmpFile = new File(outputDir + "/" + entry.getName());
                // 创建输出目录
                createDirectory(tmpFile.getParent() + "/", null);
                OutputStream out = null;
                try {
                    out = new FileOutputStream(tmpFile);
                    int length = 0;
                    byte[] b = new byte[2048];
                    while ((length = tarIn.read(b)) != -1) {
                        out.write(b, 0, length);
                    }
                } catch (IOException ex) {
                    throw ex;
                } finally {
                    if (out != null)
                        out.close();
                }
            }
        }
    } catch (IOException ex) {
        throw new IOException("解压归档文件出现异常", ex);
    } finally {
        try {
            if (tarIn != null) {
                tarIn.close();
            }
        } catch (IOException ex) {
            throw new IOException("关闭tarFile出现异常", ex);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TarInputStream(org.apache.tools.tar.TarInputStream) TarEntry(org.apache.tools.tar.TarEntry) ZipFile(java.util.zip.ZipFile)

Example 8 with TarInputStream

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

the class ImportingUtilities method explodeArchive.

public static boolean explodeArchive(File rawDataDir, InputStream archiveIS, JSONObject archiveFileRecord, JSONArray fileRecords, final Progress progress) {
    if (archiveIS instanceof TarInputStream) {
        TarInputStream tis = (TarInputStream) archiveIS;
        try {
            TarEntry te;
            while (!progress.isCanceled() && (te = tis.getNextEntry()) != null) {
                if (!te.isDirectory()) {
                    String fileName2 = te.getName();
                    File file2 = allocateFile(rawDataDir, fileName2);
                    progress.setProgress("Extracting " + fileName2, -1);
                    JSONObject fileRecord2 = new JSONObject();
                    JSONUtilities.safePut(fileRecord2, "origin", JSONUtilities.getString(archiveFileRecord, "origin", null));
                    JSONUtilities.safePut(fileRecord2, "declaredEncoding", (String) null);
                    JSONUtilities.safePut(fileRecord2, "declaredMimeType", (String) null);
                    JSONUtilities.safePut(fileRecord2, "fileName", fileName2);
                    JSONUtilities.safePut(fileRecord2, "archiveFileName", JSONUtilities.getString(archiveFileRecord, "fileName", null));
                    JSONUtilities.safePut(fileRecord2, "location", getRelativePath(file2, rawDataDir));
                    JSONUtilities.safePut(fileRecord2, "size", saveStreamToFile(tis, file2, null));
                    postProcessSingleRetrievedFile(file2, fileRecord2);
                    JSONUtilities.append(fileRecords, fileRecord2);
                }
            }
        } catch (IOException e) {
            // TODO: what to do?
            e.printStackTrace();
        }
        return true;
    } else if (archiveIS instanceof ZipInputStream) {
        ZipInputStream zis = (ZipInputStream) archiveIS;
        try {
            ZipEntry ze;
            while (!progress.isCanceled() && (ze = zis.getNextEntry()) != null) {
                if (!ze.isDirectory()) {
                    String fileName2 = ze.getName();
                    File file2 = allocateFile(rawDataDir, fileName2);
                    progress.setProgress("Extracting " + fileName2, -1);
                    JSONObject fileRecord2 = new JSONObject();
                    JSONUtilities.safePut(fileRecord2, "origin", JSONUtilities.getString(archiveFileRecord, "origin", null));
                    JSONUtilities.safePut(fileRecord2, "declaredEncoding", (String) null);
                    JSONUtilities.safePut(fileRecord2, "declaredMimeType", (String) null);
                    JSONUtilities.safePut(fileRecord2, "fileName", fileName2);
                    JSONUtilities.safePut(fileRecord2, "archiveFileName", JSONUtilities.getString(archiveFileRecord, "fileName", null));
                    JSONUtilities.safePut(fileRecord2, "location", getRelativePath(file2, rawDataDir));
                    JSONUtilities.safePut(fileRecord2, "size", saveStreamToFile(zis, file2, null));
                    postProcessSingleRetrievedFile(file2, fileRecord2);
                    JSONUtilities.append(fileRecords, fileRecord2);
                }
            }
        } catch (IOException e) {
            // TODO: what to do?
            e.printStackTrace();
        }
        return true;
    }
    return false;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) JSONObject(org.json.JSONObject) TarInputStream(org.apache.tools.tar.TarInputStream) ZipEntry(java.util.zip.ZipEntry) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) File(java.io.File)

Example 9 with TarInputStream

use of org.apache.tools.tar.TarInputStream in project ant by apache.

the class TarResource method getInputStream.

/**
 * Return an InputStream for reading the contents of this Resource.
 * @return an InputStream object.
 * @throws IOException if the tar file cannot be opened,
 *         or the entry cannot be read.
 */
@Override
public InputStream getInputStream() throws IOException {
    if (isReference()) {
        return getRef().getInputStream();
    }
    Resource archive = getArchive();
    final TarInputStream i = new TarInputStream(archive.getInputStream());
    TarEntry te;
    while ((te = i.getNextEntry()) != null) {
        if (te.getName().equals(getName())) {
            return i;
        }
    }
    FileUtils.close(i);
    throw new BuildException("no entry " + getName() + " in " + getArchive());
}
Also used : TarInputStream(org.apache.tools.tar.TarInputStream) Resource(org.apache.tools.ant.types.Resource) TarEntry(org.apache.tools.tar.TarEntry) BuildException(org.apache.tools.ant.BuildException)

Example 10 with TarInputStream

use of org.apache.tools.tar.TarInputStream in project ant by apache.

the class TarScanner method fillMapsFromArchive.

/**
 * Fills the file and directory maps with resources read from the
 * archive.
 *
 * @param src the archive to scan.
 * @param encoding encoding used to encode file names inside the archive.
 * @param fileEntries Map (name to resource) of non-directory
 * resources found inside the archive.
 * @param matchFileEntries Map (name to resource) of non-directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 * @param dirEntries Map (name to resource) of directory
 * resources found inside the archive.
 * @param matchDirEntries Map (name to resource) of directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 */
protected void fillMapsFromArchive(Resource src, String encoding, Map<String, Resource> fileEntries, Map<String, Resource> matchFileEntries, Map<String, Resource> dirEntries, Map<String, Resource> matchDirEntries) {
    try (TarInputStream ti = new TarInputStream(src.getInputStream(), encoding)) {
        try {
            TarEntry entry = null;
            while ((entry = ti.getNextEntry()) != null) {
                Resource r = new TarResource(src, entry);
                String name = entry.getName();
                if (entry.isDirectory()) {
                    name = trimSeparator(name);
                    dirEntries.put(name, r);
                    if (match(name)) {
                        matchDirEntries.put(name, r);
                    }
                } else {
                    fileEntries.put(name, r);
                    if (match(name)) {
                        matchFileEntries.put(name, r);
                    }
                }
            }
        } catch (IOException ex) {
            throw new BuildException("problem reading " + srcFile, ex);
        }
    } catch (IOException ex) {
        throw new BuildException("problem opening " + srcFile, ex);
    }
}
Also used : TarInputStream(org.apache.tools.tar.TarInputStream) TarResource(org.apache.tools.ant.types.resources.TarResource) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) TarResource(org.apache.tools.ant.types.resources.TarResource)

Aggregations

TarInputStream (org.apache.tools.tar.TarInputStream)12 TarEntry (org.apache.tools.tar.TarEntry)10 IOException (java.io.IOException)5 File (java.io.File)4 BuildException (org.apache.tools.ant.BuildException)4 FileInputStream (java.io.FileInputStream)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ArArchiveEntry (org.apache.commons.compress.archivers.ar.ArArchiveEntry)2 ArArchiveInputStream (org.apache.commons.compress.archivers.ar.ArArchiveInputStream)2 Resource (org.apache.tools.ant.types.Resource)2 BufferedInputStream (java.io.BufferedInputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipInputStream (java.util.zip.ZipInputStream)1 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)1 XZCompressorInputStream (org.apache.commons.compress.compressors.xz.XZCompressorInputStream)1