本文整理匯總了Java中org.apache.commons.compress.archivers.tar.TarArchiveInputStream.close方法的典型用法代碼示例。如果您正苦於以下問題:Java TarArchiveInputStream.close方法的具體用法?Java TarArchiveInputStream.close怎麽用?Java TarArchiveInputStream.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.compress.archivers.tar.TarArchiveInputStream
的用法示例。
在下文中一共展示了TarArchiveInputStream.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: unTar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private boolean unTar(TarArchiveInputStream tarIn, String outputDir) throws IOException {
ArchiveEntry entry;
boolean newFile = false;
while ((entry = tarIn.getNextEntry()) != null) {
File tmpFile = new File(outputDir + "/" + entry.getName());
newFile = tmpFile.createNewFile();
OutputStream out = new FileOutputStream(tmpFile);
int length;
byte[] b = new byte[2048];
while ((length = tarIn.read(b)) != -1)
out.write(b, 0, length);
out.close();
}
tarIn.close();
return newFile;
}
示例2: addTarGzipToArchive
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/**
* given tar.gz file will be copied to this tar.gz file.
* all files will be transferred to new tar.gz file one by one.
* original directory structure will be kept intact
*
* @param tarGzipFile the archive file to be copied to the new archive
*/
public boolean addTarGzipToArchive(String tarGzipFile) {
try {
// construct input stream
InputStream fin = Files.newInputStream(Paths.get(tarGzipFile));
BufferedInputStream in = new BufferedInputStream(fin);
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzIn);
// copy the existing entries from source gzip file
ArchiveEntry nextEntry;
while ((nextEntry = tarInputStream.getNextEntry()) != null) {
tarOutputStream.putArchiveEntry(nextEntry);
IOUtils.copy(tarInputStream, tarOutputStream);
tarOutputStream.closeArchiveEntry();
}
tarInputStream.close();
return true;
} catch (IOException ioe) {
LOG.log(Level.SEVERE, "Archive File can not be added: " + tarGzipFile, ioe);
return false;
}
}
示例3: untar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public static List<String> untar(FileInputStream input, String targetPath) throws IOException {
TarArchiveInputStream tarInput = new TarArchiveInputStream(input);
ArrayList<String> result = new ArrayList<String>();
ArchiveEntry entry;
while ((entry = tarInput.getNextEntry()) != null) {
File destPath=new File(targetPath,entry.getName());
result.add(destPath.getPath());
if (!entry.isDirectory()) {
FileOutputStream fout=new FileOutputStream(destPath);
final byte[] buffer=new byte[8192];
int n=0;
while (-1 != (n=tarInput.read(buffer))) {
fout.write(buffer,0,n);
}
fout.close();
}
else {
destPath.mkdir();
}
}
tarInput.close();
return result;
}
示例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: 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;
}
}
示例6: 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) {}
}
示例7: 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;
}
示例8: 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();
}
示例9: extract
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public void extract(File dir ) throws IOException {
File listDir[] = dir.listFiles();
if (listDir.length!=0){
for (File i:listDir){
/* Warning! this will try and extract all files in the directory
if other files exist, a for loop needs to go here to check that
the file (i) is an archive file before proceeding */
if (i.isDirectory()){
break;
}
String fileName = i.toString();
String tarFileName = fileName +".tar";
FileInputStream instream= new FileInputStream(fileName);
GZIPInputStream ginstream =new GZIPInputStream(instream);
FileOutputStream outstream = new FileOutputStream(tarFileName);
byte[] buf = new byte[1024];
int len;
while ((len = ginstream.read(buf)) > 0)
{
outstream.write(buf, 0, len);
}
ginstream.close();
outstream.close();
//There should now be tar files in the directory
//extract specific files from tar
TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(tarFileName));
TarArchiveEntry entry = null;
int offset;
FileOutputStream outputFile=null;
//read every single entry in TAR file
while ((entry = myTarFile.getNextTarEntry()) != null) {
//the following two lines remove the .tar.gz extension for the folder name
fileName = i.getName().substring(0, i.getName().lastIndexOf('.'));
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
File outputDir = new File(i.getParent() + "/" + fileName + "/" + entry.getName());
if(! outputDir.getParentFile().exists()){
outputDir.getParentFile().mkdirs();
}
//if the entry in the tar is a directory, it needs to be created, only files can be extracted
if(entry.isDirectory()){
outputDir.mkdirs();
}else{
byte[] content = new byte[(int) entry.getSize()];
offset=0;
myTarFile.read(content, offset, content.length - offset);
outputFile=new FileOutputStream(outputDir);
IOUtils.write(content,outputFile);
outputFile.close();
}
}
//close and delete the tar files, leaving the original .tar.gz and the extracted folders
myTarFile.close();
File tarFile = new File(tarFileName);
tarFile.delete();
}
}
}
示例10: unTar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
Log.i(TAG, String.format("Untaring %s to dir %s", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
if (!outputDir.exists()) {
outputDir.mkdirs();
}
final List<File> untaredFiles = new LinkedList<File>();
final InputStream is = new FileInputStream(inputFile);
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
if (!outputFile.exists()) {
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
untaredFiles.add(outputFile);
}
debInputStream.close();
return untaredFiles;
}
示例11: unTar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/** Untar an input file into an output file.
* The output file is created in the output folder, having the same name
* as the input file, minus the '.tar' extension.
*
* @param inputFile the input .tar file
* @param outputDir the output directory file.
* @throws IOException
* @throws FileNotFoundException
*
* @return The {@link List} of {@link File}s with the untared content.
* @throws ArchiveException
*/
private List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
_log.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
final List<File> untaredFiles = new LinkedList<File>();
final InputStream is = new FileInputStream(inputFile);
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
_log.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
_log.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
_log.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
untaredFiles.add(outputFile);
}
debInputStream.close();
return untaredFiles;
}
示例12: unTar
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/** Untar an input file into an output file.
* The output file is created in the output folder, having the same name
* as the input file, minus the '.tar' extension.
*
* @param inputFile the input .tar file
* @param outputDir the output directory file.
* @throws IOException
* @throws FileNotFoundException
*
* @return The {@link List} of {@link File}s with the untared content.
* @throws ArchiveException
*/
public static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
LOG.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
final List<File> untaredFiles = new LinkedList<File>();
final InputStream is = new FileInputStream(inputFile);
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
LOG.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOG.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
LOG.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
untaredFiles.add(outputFile);
}
debInputStream.close();
return untaredFiles;
}
示例13: readGzipStreamAsString
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
private static String readGzipStreamAsString(InputStream is) throws Exception {
TarArchiveInputStream tarIn = new TarArchiveInputStream(is);
try {
TarArchiveEntry tarEntry;
while ((tarEntry = tarIn.getNextTarEntry()) != null) {
if (tarEntry.isFile() && tarEntry.getName().endsWith(".txt")) {
return IOUtils.toString(tarIn, "UTF-8");
}
}
} finally {
tarIn.close();
}
return Strings.EMPTY;
}
示例14: extractTarGz
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
public static void extractTarGz(InputStream inputTarGzStream, String outDir,
boolean logging) {
try {
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(
inputTarGzStream);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
// read Tar entries
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
if (logging) {
LOG.info("Extracting: " + outDir + File.separator + entry.getName());
}
if (entry.isDirectory()) { // create directory
File f = new File(outDir + File.separator + entry.getName());
f.mkdirs();
} else { // decompress file
int count;
byte data[] = new byte[EXTRACT_BUFFER_SIZE];
FileOutputStream fos = new FileOutputStream(outDir + File.separator
+ entry.getName());
BufferedOutputStream dest = new BufferedOutputStream(fos,
EXTRACT_BUFFER_SIZE);
while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) {
dest.write(data, 0, count);
}
dest.close();
}
}
// close input stream
tarIn.close();
} catch (IOException e) {
LOG.error("IOException: " + e.getMessage());
}
}
示例15: storeOperator
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; //導入方法依賴的package包/類
/**
* Stores an operator from a tarball
* */
private void storeOperator(InputStream is, String outputDir) throws Exception {
logger.info("Writting operator to: "+outputDir);
File file = new File(outputDir);
file.mkdir();
TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
logger.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
logger.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
logger.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
}
debInputStream.close();
}