【问题标题】:Java: How to perform dos2unix on multiple xml files in a zip file?Java:如何对 zip 文件中的多个 xml 文件执行 dos2unix?
【发布时间】:2019-12-31 02:34:32
【问题描述】:

我有一个表单来上传一个 zip 文件,然后将 zip 中的所有 xml 文件转换为 unix 格式(如果它们是 dos 格式)。现在我将输入作为 InputStream 接收。如何处理输入流中的文件并对其执行 (dos2unix) 以将其转换为 unix 格式?

我尝试将流转换为文件,然后再转换,但没有成功

public void uploadFile(UploadAuditConfig transaction,String fileType, InputStream in, String delimiter) {
    ZipInputStream zipInputStream = new ZipInputStream(in);
    ZipEntry entry = null;
    do{
                entry = zipInputStream.getNextEntry();
                //need to convert this entry to unix format if it is dos before I pass it to processFile method
                if(entry != null && !entry.isDirectory()) {
                    List<Map<String,String>> list =processFile(zipInputStream, delimiter);
                    zipInputStream.closeEntry();
                 }
    }while(entry!=null);
}


public List<Map<String, String>> processFile(InputStream in, String 
delimiter){
        List<Map<String,String>> acesList = new ArrayList<>();
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new InputStreamReader(in));
        while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        File file = new File("/tmp/" + "out" + i + ".xml");
                FileWriter fw = new FileWriter(file);
                if (!file.exists())
                    file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file, true);
                t.transform(new StAXSource(xsr), new StreamResult(fos));
                fos.close();

                if (i == 0) {
                    JSONObject xmlJSONObjHeader = XML.toJSONObject(content);
                    Object o = JsonPath.parse(xmlJSONObjHeader.toString()).read("$['Header']['BrandAAIAID']");
                    brandaaiaid = String.valueOf(o);
                    logger.info("Brand id: " + brandaaiaid);
                    file.delete();
                    fw.close();
                    i++;


                }
        }
        return acesList;
}

预期:来自输入流的 Unix 格式文件

【问题讨论】:

  • 你应该如何处理转换的结果?
  • 这是完整的代码吗? processFile() 的返回类型为 List&lt;Map&lt;String, String&gt;&gt;,但没有 return 语句。
  • 为什么? XML 不关心,因此它的任何标准工具或 API 也不关心,包括您正在使用的那些。你不需要这样做。
  • 怎么失败了?这是真正的问题。你拿锤子敲碎坚果。不清楚您是否甚至需要XMLStreamReader。并摆脱exists()/createNewFile() 的东西。这完全是浪费时间和空间,而且无论如何你都是在错误的地方做的。如果它有效,您将获得空文件。
  • 我只能重复一遍。 XML 不在乎。您引用的错误是因为您已经使用了流的第一个元素,因此 SAX 解析器无法解析它的其余部分。它与 DOS2UNIX 无关。您的代码是错误的,正如我已经说过的,您根本不需要XMLStreamReader。只需从 zip 输入流创建一个StreamSource

标签: java inputstream dos2unix


【解决方案1】:

我能够将输入流转换为文件,然后使用 dos2unix 将文件转换为 unix,然后再次将文件作为输入流传递。

    OutputStream outputStream = null;
    File file1 = new File("/tmp/output.txt");
    try
    {
        outputStream = new FileOutputStream(file1);

        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = in1.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
         } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally
        {
            if(outputStream != null)
            { 
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    ExecuteShell.executeJavaCommand(file1.getAbsolutePath());

    logger.info("action=output.txt converted from dos to unix");

    InputStream in = null;
    try {
        in = new FileInputStream(file1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

【讨论】:

  • 我敦促您研究问题的根本原因,因为 XML 通常不关心行尾。您作为答案发布的代码比它的价值更麻烦。它吞没了异常。它不是线程安全的(可能是也可能不是问题,但如果是,请享受调试的乐趣)。它过于复杂,写入和读取临时文件以及转换行尾的额外过程。它依赖于存在的外部工具。您的答案也不是一个完整的可重用示例,因此其他人不太可能从中受益;参考How to Answer
  • 我发现了问题,所以基本上当我尝试使用 xmlStreamReader 读取标签时,如果文件是 dos 格式,它会失败,当我使用上面的代码将输入流转换为 unix 格式时,它修复了问题。因此我发布了解决方案,因为我认为它会帮助面临同样问题的人
  • 没有。这不是一个解决方案,而是一种快速而肮脏的黑客攻击,一种解决方法。你没有找到真正的问题。阅读您的问题下的 cmets。我不允许您签入该代码。不过,这取决于您。
  • @Robert 我确实找到了真正的问题。该文件具有 BOM 格式,转换后它删除了前三个字节并有助于读取标签。
猜你喜欢
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
相关资源
最近更新 更多