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