كيف يمكنني استخراج الملف tar في جافا ؟
سؤال
كيف يمكنني استخراج القطران (أو tar.gz أو القطران.bz2) الملف في جافا ؟
المحلول
ملاحظة: هذه الوظيفة كانت نشرت في وقت لاحق من خلال مشروع منفصل ، أباتشي العموم ضغط ، ووصف في جواب آخر. هذا الجواب هو خارج التاريخ.
أنا لم استخدم القطران API مباشرة ، ولكن القطران و bzip2 يتم تنفيذها في النمل;هل يمكن أن تقترض تنفيذها ، أو ربما استخدام النمل أن تفعل ما عليك.
Gzip هو جزء من جافا SE (و أظن أن النمل تنفيذ يتبع نفس النموذج).
GZIPInputStream
هو مجرد InputStream
الديكور.يمكنك لف ، على سبيل المثال ، FileInputStream
في GZIPInputStream
واستخدامها بنفس الطريقة يمكنك أن تستخدم أي InputStream
:
InputStream is = new GZIPInputStream(new FileInputStream(file));
(لاحظ أن GZIPInputStream لديها قناعاتها الداخلية العازلة ، لذلك التفاف FileInputStream
في BufferedInputStream
من المحتمل انخفاض الأداء.)
نصائح أخرى
ويمكنك القيام بذلك مع مكتبة أباتشي العموم ضغط. يمكنك تحميل الإصدار 1.2 من http://mvnrepository.com/artifact/ org.apache.commons / المشاعات، ضغط / 1.2 .
وهنا طريقتين: الأولى أن unzips ملف واحد آخر untars ذلك. لذلك، على ملف <اسم الملف> tar.gz، تحتاج إلى أولا فك الضغط وبعد ذلك untar هو. يرجى ملاحظة أن أرشيف القطران قد تحتوي المجلدات كذلك، الحالة التي هم بحاجة إلى أن تنشأ على الملفات المحلية.
واستمتع.
/** 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 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;
}
/**
* Ungzip an input file into an output file.
* <p>
* The output file is created in the output folder, having the same name
* as the input file, minus the '.gz' extension.
*
* @param inputFile the input .gz file
* @param outputDir the output directory file.
* @throws IOException
* @throws FileNotFoundException
*
* @return The {@File} with the ungzipped content.
*/
private static File unGzip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {
LOG.info(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
final File outputFile = new File(outputDir, inputFile.getName().substring(0, inputFile.getName().length() - 3));
final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
final FileOutputStream out = new FileOutputStream(outputFile);
IOUtils.copy(in, out);
in.close();
out.close();
return outputFile;
}
أباتشي المشاع "في إف إس" يدعم القطران كما نظام الملفات الافتراضية, الذي يدعم عناوين مثل هذا واحد tar:gz:http://anyhost/dir/mytar.tar.gz!/mytar.القطران!/path/in/tar/README.txt
TrueZip أو خليفتها TrueVFS يفعل نفس الشيء ...كما أنها متاحة من مخضرم المركزية.
Archiver archiver = ArchiverFactory.createArchiver("tar", "gz");
archiver.extract(archiveFile, destDir);
والتبعية:
<dependency>
<groupId>org.rauschig</groupId>
<artifactId>jarchivelib</artifactId>
<version>0.5.0</version>
</dependency>
وحاولت فقط حفنة من يبس اقترح (TrueZip، اباتشي ضغط)، ولكن لا حظ.
وهنا مثال مع اباتشي العموم VFS:
FileSystemManager fsManager = VFS.getManager();
FileObject archive = fsManager.resolveFile("tgz:file://" + fileName);
// List the children of the archive file
FileObject[] children = archive.getChildren();
System.out.println("Children of " + archive.getName().getURI()+" are ");
for (int i = 0; i < children.length; i++) {
FileObject fo = children[i];
System.out.println(fo.getName().getBaseName());
if (fo.isReadable() && fo.getType() == FileType.FILE
&& fo.getName().getExtension().equals("nxml")) {
FileContent fc = fo.getContent();
InputStream is = fc.getInputStream();
}
}
والاعتماد مخضرم:
<dependency>
<groupId>commons-vfs</groupId>
<artifactId>commons-vfs</artifactId>
<version>1.0</version>
</dependency>
وبالإضافة إلى غزيب وBZIP2، أباتشي العموم ضغط API لديها لدعم القطران، في الأصل على أساس ICE الهندسة جافا قطران حزمة ، التي هي على حد سواء API وأداة قائمة بذاتها.
وإليك نسخة بناء على وقت سابق من هذا الجواب دان Borza أن الاستخدامات <لأ href = "HTTPS: // mvnrepository كوم / قطعة أثرية / org.apache.commons / المشاعات، ضغط / 1.18 "يختلط =" نوفولو noreferrer "> أباتشي العموم ضغط و جافا NIO (أي مسار بدلا من ملف). وهو يفعل أيضا فك الضغط وuntarring في تيار واحد لذلك ليس هناك إنشاء ملف المتوسط.
public static void unTarGz( Path pathInput, Path pathOutput ) throws IOException {
TarArchiveInputStream tararchiveinputstream =
new TarArchiveInputStream(
new GzipCompressorInputStream(
new BufferedInputStream( Files.newInputStream( pathInput ) ) ) );
ArchiveEntry archiveentry = null;
while( (archiveentry = tararchiveinputstream.getNextEntry()) != null ) {
Path pathEntryOutput = pathOutput.resolve( archiveentry.getName() );
if( archiveentry.isDirectory() ) {
if( !Files.exists( pathEntryOutput ) )
Files.createDirectory( pathEntryOutput );
}
else
Files.copy( tararchiveinputstream, pathEntryOutput );
}
tararchiveinputstream.close();
}