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

求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条回答 默认 最新

相关推荐 更多相似问题