javainputstreamdos2unix

Java: How to perform dos2unix on multiple xml files in a zip file?


I have a form to upload a zip file and then convert all the xml files inside the zip to unix format if they are in dos format. Right now I receive the input as an InputStream. How do I process the file in inputstream and perform (dos2unix) on it to convert it to unix format?

I tried to convert the stream to a file and then convert them but didn't work

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;
}

Expected: Unix formatted file from inputstream


Solution

  • I was able to convert the inputstream to a file and then convert the file to unix using dos2unix and then again passing the file as inputstream.

        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();
        }