要幸福 2013-04-23 17:28
浏览 276
已采纳

求java解析tar.Z? 另附上java对zip、tar.gz、 tar.bz、 tar的解压。

       使用java代码解tar.Z文件,程序最终跑在windows上面,不排除以后客户迁移,所以不能使用java调用系统命令的方式解压,只能通过代码来解压。java内部支持解压格式太少,后来使用apache.commons.compress解压了zip、tar.gz、 tar.bz、 tar。但是还是没有找到tar.Z怎么解压,求教高手,帮忙解答。下面是zip、tar.gz、 tar.bz、 tar几种格式的解压。       

 

public List<String> list(InputStream inputStream, boolean isCloseStream) throws IOException {
        List<String> list = new ArrayList<String>();
        ArchiveInputStream in = null;
        try {
            in = new ArchiveStreamFactory().createArchiveInputStream("tar", new BufferedInputStream(inputStream));
            TarArchiveEntry entry = null;
            while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
                list.addAll(FileFach.parserEveryFile(entry.getName().replaceAll("\\\\", "/"), in));
            }
        } catch (ArchiveException e) {
            throw new ExtractorException(ErrorType.UNSUPPORTED_FILE_TYPE, e);
        } finally {
            if (isCloseStream) {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(inputStream);
            }
        }
        return list;
    }
public List<String> list(InputStream inputStream, boolean isCloseStream) throws IOException {
        List<String> list = new ArrayList<String>();
        ArchiveInputStream in = null;
        GZIPInputStream gis = null;
        try {
            gis = new GZIPInputStream(new BufferedInputStream(inputStream));
            in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis);            
            TarArchiveEntry entry = null;

            while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
                list.addAll(FileFach.parserEveryFile(entry.getName().replaceAll("\\\\", "/"), in));
            }
            entry = null;
        } catch (ArchiveException e) {
            throw new ExtractorException(ErrorType.UNSUPPORTED_FILE_TYPE, e);
        } finally {
            if (isCloseStream) {
                IOUtils.closeQuietly(gis);
                IOUtils.closeQuietly(inputStream);
                IOUtils.closeQuietly(in);
            }
        }
        return list;
    }
public List<String> list(InputStream inputStream, boolean isCloseStream) throws IOException {

        List<String> list = new ArrayList<String>();
        ArchiveInputStream in = null;
        try {
            in = new ArchiveStreamFactory().createArchiveInputStream("tar", new BZip2CompressorInputStream(inputStream));
            TarArchiveEntry entry = null;
            while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
                list.addAll(FileFach.parserEveryFile(entry.getName().replaceAll("\\\\", "/"), in));
            }
        } catch (ArchiveException e) {
            throw new ExtractorException(ErrorType.UNSUPPORTED_FILE_TYPE, e);
        } finally {
            if (isCloseStream) {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(inputStream);
            }
        }
        return list;
    }
public List<String> list(InputStream inputStream, boolean isCloseStream) throws IOException {
        List<String> list = new ArrayList<String>();
        ArchiveInputStream in = null;
        try {
            in = new ArchiveStreamFactory().createArchiveInputStream("zip", new BufferedInputStream(inputStream));
            ZipArchiveEntry entry = null;
            while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {
                list.addAll(FileFach.parserEveryFile(entry.getName().replaceAll("\\\\", "/"), in));
            }
        } catch (ArchiveException e) {
            throw new ExtractorException(ErrorType.UNSUPPORTED_FILE_TYPE, e);
        } finally {
            if (isCloseStream) {
                IOUtils.closeQuietly(inputStream);
                IOUtils.closeQuietly(in);
            }
        }
        return list;
    }
  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办