本文整理匯總了Java中org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry方法的典型用法代碼示例。如果您正苦於以下問題:Java TarArchiveInputStream.getNextTarEntry方法的具體用法?Java TarArchiveInputStream.getNextTarEntry怎麽用?Java TarArchiveInputStream.getNextTarEntry使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.compress.archivers.tar.TarArchiveInputStream
的用法示例。
在下文中一共展示了TarArchiveInputStream.getNextTarEntry方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: untar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public static void untar(InputStream in, File targetDir) throws IOException {
final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
byte[] b = new byte[BUF_SIZE];
TarArchiveEntry tarEntry;
while ((tarEntry = tarIn.getNextTarEntry()) != null) {
final File file = new File(targetDir, tarEntry.getName());
if (tarEntry.isDirectory()) {
if (!file.mkdirs()) {
throw new IOException("Unable to create folder " + file.getAbsolutePath());
}
} else {
final File parent = file.getParentFile();
if (!parent.exists()) {
if (!parent.mkdirs()) {
throw new IOException("Unable to create folder " + parent.getAbsolutePath());
}
}
try (FileOutputStream fos = new FileOutputStream(file)) {
int r;
while ((r = tarIn.read(b)) != -1) {
fos.write(b, 0, r);
}
}
}
}
}
示例2: verifyTarArchive
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/**
* Helper method to verify that the files were archived correctly by reading {@code
* tarArchiveInputStream}.
*/
private void verifyTarArchive(TarArchiveInputStream tarArchiveInputStream) throws IOException {
// Verifies fileA was archived correctly.
TarArchiveEntry headerFileA = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("some/path/to/resourceFileA", headerFileA.getName());
String fileAString =
CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(expectedFileAString, fileAString);
// Verifies fileB was archived correctly.
TarArchiveEntry headerFileB = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("crepecake", headerFileB.getName());
String fileBString =
CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(expectedFileBString, fileBString);
// Verifies directoryA was archived correctly.
TarArchiveEntry headerDirectoryA = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("some/path/to/", headerDirectoryA.getName());
Assert.assertNull(tarArchiveInputStream.getNextTarEntry());
}
示例3: dearchive
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/**
* 文件 解歸檔
*
* @param destFile
* 目標文件
* @param tais
* ZipInputStream
* @throws Exception
*/
private static void dearchive(File destFile, TarArchiveInputStream tais)
throws Exception {
TarArchiveEntry entry = null;
while ((entry = tais.getNextTarEntry()) != null) {
// 文件
String dir = destFile.getPath() + File.separator + entry.getName();
File dirFile = new File(dir);
// 文件檢查
fileProber(dirFile);
if (entry.isDirectory()) {
dirFile.mkdirs();
} else {
dearchiveFile(dirFile, tais);
}
}
}
示例4: run
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
@Override
public void run(Parameters p, PrintStream output) throws Exception {
TarArchiveInputStream tais = new TarArchiveInputStream(StreamCreator.openInputStream(p.getString("input")));
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(p.getString("output")));
boolean quiet = p.get("quiet", false);
while(true) {
TarArchiveEntry tarEntry = tais.getNextTarEntry();
if(tarEntry == null) break;
if(!tarEntry.isFile()) continue;
if(!tais.canReadEntryData(tarEntry)) continue;
if(!quiet) System.err.println("# "+tarEntry.getName());
ZipEntry forData = new ZipEntry(tarEntry.getName());
forData.setSize(tarEntry.getSize());
zos.putNextEntry(forData);
StreamUtil.copyStream(tais, zos);
zos.closeEntry();
}
tais.close();
zos.close();
}
示例5: untarFile
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public static void untarFile(File file) throws IOException {
FileInputStream fileInputStream = null;
String currentDir = file.getParent();
try {
fileInputStream = new FileInputStream(file);
GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream);
TarArchiveEntry tarArchiveEntry;
while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
if (tarArchiveEntry.isDirectory()) {
FileUtils.forceMkdir(new File(currentDir + File.separator + tarArchiveEntry.getName()));
} else {
byte[] content = new byte[(int) tarArchiveEntry.getSize()];
int offset = 0;
tarArchiveInputStream.read(content, offset, content.length - offset);
FileOutputStream outputFile = new FileOutputStream(currentDir + File.separator + tarArchiveEntry.getName());
org.apache.commons.io.IOUtils.write(content, outputFile);
outputFile.close();
}
}
} catch (FileNotFoundException e) {
throw new IOException(e.getStackTrace().toString());
}
}
示例6: findLastTarEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private InputStream findLastTarEntry() throws IOException {
SeekableXZInputStream stream = (SeekableXZInputStream)xz.stream();
stream.seekToBlock(stream.getBlockCount() - 2);
byte[] buffer = new byte[(int)(stream.length() - stream.position())];
IOUtils.readFully(stream, buffer);
ByteArrayInputStream memoryStream = new ByteArrayInputStream(buffer);
TarArchiveInputStream tar = new TarArchiveInputStream(memoryStream);
for (int i = 0; i < (buffer.length / TarConstants.DEFAULT_RCDSIZE); i++) {
TarArchiveEntry entry = null;
memoryStream.reset();
memoryStream.skip(i * TarConstants.DEFAULT_RCDSIZE);
tar.reset();
try {
entry = tar.getNextTarEntry();
} catch (IOException ex) {
}
if (entry != null && entry.getName().equals(FOOTER_NAME)) {
return tar;
}
}
throw new IOException("Invalid file format");
}
示例7: parse
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
@Override
public void parse(File file) throws IOException {
mEntries = new ArrayList<>();
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
TarArchiveInputStream is = new TarArchiveInputStream(fis);
TarArchiveEntry entry = is.getNextTarEntry();
while (entry != null) {
if (entry.isDirectory()) {
continue;
}
if (Utils.isImage(entry.getName())) {
mEntries.add(new TarEntry(entry, Utils.toByteArray(is)));
}
entry = is.getNextTarEntry();
}
Collections.sort(mEntries, new NaturalOrderComparator() {
@Override
public String stringValue(Object o) {
return ((TarEntry) o).entry.getName();
}
});
}
示例8: getBufferedReaderTarGz
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/**
* Gets a Reader for a file in a gzipped tarball
* @param tarPath Path to the tarball
* @param fileName File within the tarball
* @return The file's reader
*/
public static BufferedReader getBufferedReaderTarGz(final String tarPath, final String fileName) {
try {
InputStream result = null;
final TarArchiveInputStream tarStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarPath)));
TarArchiveEntry entry = tarStream.getNextTarEntry();
while (entry != null) {
if (entry.getName().equals(fileName)) {
result = tarStream;
break;
}
entry = tarStream.getNextTarEntry();
}
if (result == null) {
throw new UserException.BadInput("Could not find file " + fileName + " in tarball " + tarPath);
}
return new BufferedReader(new InputStreamReader(result));
} catch (final IOException e) {
throw new UserException.BadInput("Could not open compressed tarball file " + fileName + " in " + tarPath, e);
}
}
示例9: getHeader
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public byte[] getHeader() {
try {
TarArchiveEntry entry=null;
TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(sinfile)));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ((entry = tarIn.getNextTarEntry()) != null) {
if (entry.getName().endsWith("cms")) {
IOUtils.copy(tarIn, bout);
break;
}
}
tarIn.close();
return bout.toByteArray();
} catch (Exception e) {
return null;
}
}
示例10: dumpImage
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public void dumpImage() {
try {
TarArchiveEntry entry=null;
TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(sinfile)));
FileOutputStream fout = new FileOutputStream(new File("D:\\test.ext4"));
while ((entry = tarIn.getNextTarEntry()) != null) {
if (!entry.getName().endsWith("cms")) {
IOUtils.copy(tarIn, fout);
}
}
tarIn.close();
fout.flush();
fout.close();
logger.info("Extraction finished to "+"D:\\test.ext4");
} catch (Exception e) {}
}
示例11: getTransferVmdK
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private ITransferVmdkFrom getTransferVmdK(final String templateFilePathStr, final String vmdkName) throws IOException {
final Path templateFilePath = Paths.get(templateFilePathStr);
if (isOva(templateFilePath)) {
final TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(templateFilePathStr));
TarArchiveEntry entry;
while ((entry = tar.getNextTarEntry()) != null) {
if (new File(entry.getName()).getName().startsWith(vmdkName)) {
return new TransferVmdkFromInputStream(tar, entry.getSize());
}
}
} else if (isOvf(templateFilePath)) {
final Path vmdkPath = templateFilePath.getParent().resolve(vmdkName);
return new TransferVmdkFromFile(vmdkPath.toFile());
}
throw new RuntimeException(NOT_OVA_OR_OVF);
}
示例12: extractFileByName
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private File extractFileByName(File archive, String filenameToExtract) throws IOException {
File baseDir = new File(FileUtils.getTempDirectoryPath());
File expectedFile = new File(baseDir, filenameToExtract);
expectedFile.delete();
assertThat(expectedFile.exists(), is(false));
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream(
new BufferedInputStream(new FileInputStream(archive))));
TarArchiveEntry entry;
while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
String individualFiles = entry.getName();
// there should be only one file in this archive
assertThat(individualFiles, equalTo("executableFile.sh"));
IOUtils.copy(tarArchiveInputStream, new FileOutputStream(expectedFile));
if ((entry.getMode() & 0755) == 0755) {
expectedFile.setExecutable(true);
}
}
tarArchiveInputStream.close();
return expectedFile;
}
示例13: unpackTar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public static void unpackTar(InputStream tarArchive, File outputDirectory) throws IOException {
TarArchiveInputStream tarStream = new TarArchiveInputStream(tarArchive);
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
unpackTarArchiveEntry(tarStream, entry, outputDirectory);
}
}
示例14: unpack
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private void unpack(final @WillNotClose TarArchiveInputStream tain)
throws IOException {
final TarDriver driver = this.driver;
final IoBufferPool pool = driver.getPool();
for ( TarArchiveEntry tinEntry;
null != (tinEntry = tain.getNextTarEntry()); ) {
final String name = name(tinEntry);
TarDriverEntry entry = entries.get(name);
if (null != entry)
entry.release();
entry = driver.newEntry(name, tinEntry);
if (!tinEntry.isDirectory()) {
final IoBuffer buffer = pool.allocate();
entry.setBuffer(buffer);
try {
try (OutputStream out = buffer.output().stream(null)) {
Streams.cat(tain, out);
}
} catch (final Throwable ex) {
try {
buffer.release();
} catch (final Throwable ex2) {
ex.addSuppressed(ex2);
}
throw ex;
}
}
entries.put(name, entry);
}
}
示例15: extract
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/**
* Unpack the contents of a tarball (.tar.gz)
*
* @param tarball The source tarbal
*/
public static void extract(File tarball) throws IOException {
TarArchiveInputStream tarIn = new TarArchiveInputStream(
new GzipCompressorInputStream(
new BufferedInputStream(
new FileInputStream(tarball))));
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
while (tarEntry != null) {
File entryDestination = new File(tarball.getParent(), tarEntry.getName());
FileUtils.forceMkdirParent(entryDestination);
if (tarEntry.isDirectory()) {
FileUtils.forceMkdir(entryDestination);
} else {
entryDestination.createNewFile();
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(entryDestination));
byte[] buffer = new byte[1024];
int len;
while ((len = tarIn.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
}
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}