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;
}
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);
}
}
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();
}
}
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());
}
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);
}
}
Aggregations