Java 使用ApacheCommonsCompress解压缩tar文件
我正在使用ApacheCommonsCompress创建tar归档并解压缩它们。我的问题从这个方法开始:Java 使用ApacheCommonsCompress解压缩tar文件,java,apache-commons,apache-commons-compress,Java,Apache Commons,Apache Commons Compress,我正在使用ApacheCommonsCompress创建tar归档并解压缩它们。我的问题从这个方法开始: private void decompressFile(File file) throws IOException { logger.info("Decompressing " + file.getName()); BufferedOutputStream outputStream = null; TarArchiveInputStream tarInput
private void decompressFile(File file) throws IOException {
logger.info("Decompressing " + file.getName());
BufferedOutputStream outputStream = null;
TarArchiveInputStream tarInputStream = null;
try {
tarInputStream = new TarArchiveInputStream(
new FileInputStream(file));
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextTarEntry()) != null) {
if (!entry.isDirectory()) {
File compressedFile = entry.getFile();
File tempFile = File.createTempFile(
compressedFile.getName(), "");
byte[] buffer = new byte[BUFFER_MAX_SIZE];
outputStream = new BufferedOutputStream(
new FileOutputStream(tempFile), BUFFER_MAX_SIZE);
int count = 0;
while ((count = tarInputStream.read(buffer, 0, BUFFER_MAX_SIZE)) != -1) {
outputStream.write(buffer, 0, count);
}
}
deleteFile(file);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
}
}
每次运行代码时,compressedFile变量都为null,但while循环会迭代测试tar中的所有条目
你能帮我理解我做错了什么吗?试着用方法代替方法
第二个方法返回一个TarArchiveEntry。也许这不是你想要的 来自官方文件
从tar存档中读取条目:
TarArchiveEntry entry = tarInput.getNextTarEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
tarInput.read(content, offset, content.length - offset);
}
我已经用一个非常简单的.tar(仅一条文本)编写了一个从您的实现和测试开始的示例。由于不知道确切的要求,我只负责解决读取归档文件的问题,避免使用空指针。调试时,该条目也可用,正如您所发现的
private static void decompressFile(File file) throws IOException {
BufferedOutputStream outputStream = null;
TarArchiveInputStream tarInputStream = null;
try {
tarInputStream = new TarArchiveInputStream(
new FileInputStream(file));
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextTarEntry()) != null) {
if (!entry.isDirectory()) {
File compressedFile = entry.getFile();
String name = entry.getName();
int size = 0;
int c;
while (size < entry.getSize()) {
c = tarInputStream.read();
System.out.print((char) c);
size++;
}
(.......)
private static void解压缩文件(文件文件)引发IOException{
BufferedOutputStream outputStream=null;
TarArchiveInputStream tarInputStream=null;
试一试{
tarInputStream=新的TarArchiveInputStream(
新文件输入流(文件));
塔拉奇入口入口;
while((entry=tarInputStream.getNextTarEntry())!=null){
如果(!entry.isDirectory()){
File compressedFile=entry.getFile();
String name=entry.getName();
int size=0;
INTC;
while(size
正如我所说的:我使用只包含文本条目的tar进行了测试(您也可以尝试这种方法来验证代码),以确保避免空值。你需要根据自己的实际需要进行所有必要的调整。 很明显,您必须像我在上面发布的元代码一样处理流。
它显示了如何处理单个条目。尝试创建如下输出流:outputStream=new FileOutputStream(new File(entry.getName()));我需要创建一个临时文件。我可以使用file.createTempFile而不是新文件吗?我在下面给出了完整的回答…我正在使用tar文件,所以是的,这就是我想要的。无论如何,我稍后会尝试您的解决方案。谢谢您的回答;)压缩文件的目的是什么?