Java: 如何对 zip 文件中的多个 xml 文件执行 dos2unix?
Java: How to perform dos2unix on multiple xml files in a zip file?
我有一个表格可以上传一个 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 格式文件
我能够将输入流转换为文件,然后使用 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();
}
我有一个表格可以上传一个 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 格式文件
我能够将输入流转换为文件,然后使用 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();
}