报错:java.io.IOException: Error de

2023-10-30  本文已影响0人  Yluozi

java解压tar文件使用tarIs.getNextEntry() 方法 总报错:java.io.IOException: Error detected parsing the header

    public static void main(String[] args) {
        String ss = "F:";

        try {
//          File file = new File(ss+File.separator+"test"+File.separator+"infer_result.tar");
//          InputStream inputStream1 = new FileInputStream(file);
//          File file1 = new File(ss+File.separator+"test"+File.separator+"moni"+File.separator+"infer_result.tar");
//          inputStreamToFile(inputStream1,file1);
            log.info("解压缩的csv文件名:"+">>>执行csv文件解析入库操作...");

            FileInputStream fis = new FileInputStream(ss+File.separator+"test"+File.separator+"infer_result.tar");
            InputStream inputStream = fis;
            TarArchiveInputStream tarIs = new TarArchiveInputStream(inputStream,"UTF-8");
            tarIs.getCurrentEntry();
            TarArchiveEntry currentEntry = (TarArchiveEntry) tarIs.getNextEntry();
            while (currentEntry != null) {
                String fileName = currentEntry.getName();
                File newFile = new File(ss+File.separator+"test"+File.separator+"111" + File.separator + fileName);
                System.out.println("Extracting to " + newFile.getAbsolutePath());
                FileOutputStream fos = new FileOutputStream(newFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = tarIs.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                currentEntry = (TarArchiveEntry) tarIs.getNextEntry();
            }
            tarIs.close();
            fis.close();
            log.info("解压tar文件成功!");

        }catch (Exception e){
            System.out.println(e);
        }
    }

原因:
TarArchiveInputStream tarIs = new TarArchiveInputStream(inputStream,"UTF-8");
转码报错,不能直接从InputStream 转 TarArchiveInputStream
先使用 GzipCompressorInputStream 转一下 gzip的流不报错了

TarArchiveInputStream tarIs = new TarArchiveInputStream(new GzipCompressorInputStream(fis),"UTF-8");
上一篇 下一篇

猜你喜欢

热点阅读