Search in sources :

Example 11 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 12 with TarInputStream

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

the class Untar method expandStream.

/**
 * @since Ant 1.7
 */
private void expandStream(String name, InputStream stream, File dir) throws IOException {
    try (TarInputStream tis = new TarInputStream(compression.decompress(name, new BufferedInputStream(stream)), getEncoding())) {
        log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
        boolean empty = true;
        FileNameMapper mapper = getMapper();
        TarEntry te;
        while ((te = tis.getNextEntry()) != null) {
            empty = false;
            extractFile(FileUtils.getFileUtils(), null, dir, tis, te.getName(), te.getModTime(), te.isDirectory(), mapper);
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '%s' is empty", name);
        }
        log("expand complete", Project.MSG_VERBOSE);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) TarInputStream(org.apache.tools.tar.TarInputStream) TarEntry(org.apache.tools.tar.TarEntry) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) BuildException(org.apache.tools.ant.BuildException)

Example 13 with TarInputStream

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

the class TarTest method testTarFilesetWithSymlinks.

@Test
public void testTarFilesetWithSymlinks() throws IOException {
    buildRule.executeTarget("testTarFilesetWithSymlinks");
    final File f = new File(buildRule.getProject().getProperty("output"), "result.tar");
    final TarInputStream tis = new TarInputStream(new FileInputStream(f));
    try {
        final TarEntry e1 = tis.getNextEntry();
        assertEquals("pre/dir/file", e1.getName());
        assertEquals("", e1.getLinkName());
        assertEquals(48, e1.getLinkFlag());
        final TarEntry e2 = tis.getNextEntry();
        assertEquals("pre/sub/file", e2.getName());
        assertEquals("../dir/file", e2.getLinkName());
        assertEquals(50, e2.getLinkFlag());
        assertNull(tis.getNextEntry());
    } finally {
        tis.close();
    }
}
Also used : TarInputStream(org.apache.tools.tar.TarInputStream) TarEntry(org.apache.tools.tar.TarEntry) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 14 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 15 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)16 TarEntry (org.apache.tools.tar.TarEntry)14 IOException (java.io.IOException)8 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)4 BuildException (org.apache.tools.ant.BuildException)4 BufferedInputStream (java.io.BufferedInputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)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 IteratorReader (org.opengrok.indexer.analysis.IteratorReader)2 OGKTextField (org.opengrok.indexer.analysis.OGKTextField)2 ManipString (fr.insee.arc.utils.utils.ManipString)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1