gpt4 book ai didi

java - 如何连接从路径到目录的字符串和目录中的多个 zip 文件名?

转载 作者:行者123 更新时间:2023-11-30 02:45:49 25 4
gpt4 key购买 nike

我正在编写一些脚本来原子化我几乎每天都需要执行的手动功能。

我的第一个问题是从路径和 zip 文件名构建一个字符串。第二个问题是循环遍历 zip 文件名(路径+zip 文件名)。

这是包含多个 zip 文件的目录路径:/Users/John.Smith/Desktop/Test_script/

以下是众多 zip 文件之一的名称:CRM_CI_20161016_000001_50661561.zip

最后,我需要循环遍历每个 zip 文件名称中的目录和子字符串(数字 50661561)以便对其进行操作。

有人可以给我建议吗?

这是我在下面给出的代码,用于仅操作一个 zip 文件:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;



public class UnzipUtilityTest {
public static void main(String[] args) {



// unzip file

String zipFilePath = "/Users/John.Smith/Desktop/Test_script/CRM_CI_20161016_000001_50661561.zip";
String destDirectory = "/Users/John.Smith/Desktop/Test_script/test";
UnzipUtility unzipper = new UnzipUtility();


try {
unzipper.unzip(zipFilePath, destDirectory);
} catch (Exception ex) {

System.out.println("ERROR:Unzip did not work");
}


// read provider id
String old_prov_id = zipFilePath.substring(66, 74);
System.out.println("Old provider ID :"+old_prov_id );

// add +1 to provider ID
int new_provider_ID = Integer.parseInt(old_prov_id);
new_provider_ID++;
System.out.println("New provider ID :"+new_provider_ID );


// convert provider-id INT into String

String str_provider_id = Integer.toString(new_provider_ID);
System.out.println("New String provider ID :"+str_provider_id );

// concatenate two String into one
StringBuilder bufferPDF = new StringBuilder()
.append(new_provider_ID).append(".pdf");
System.out.println(bufferPDF.toString());

StringBuilder bufferXML = new StringBuilder()
.append(new_provider_ID).append(".xml");
System.out.println(bufferXML.toString());

// convert names of XML and PDF

Path sourcePDF = Paths.get("/Users/John.Smith/Desktop/Test_script/test/50661561.pdf");
try {
Files.move(sourcePDF, sourcePDF.resolveSibling(bufferPDF.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Path sourceXML = Paths.get("/Users/John.Smith/Desktop/Test_script/test/50661561.xml");
try {
Files.move(sourceXML, sourceXML.resolveSibling(bufferXML.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// change provider-id and filename in xml file

try {

String filepath = "/Users/John.Smith/Desktop/Test_script/test/50661562.xml";

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);

// Get the root element provider-id
Node provider = doc.getElementsByTagName("provider-id").item(0);
provider.setTextContent(str_provider_id);

// Get the root element filename
Node filename = doc.getElementsByTagName("filename").item(0);
filename.setTextContent(str_provider_id);


// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);

System.out.println("Done");

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}

}
}

最佳答案

我的处理方式有点不同:

  1. 列出目录中的所有文件
  2. 循环浏览文件列表中的项目
  3. 提取旧 ID 并递增它
  4. 处理新 ID

示例:

//you could probably make this nicer

String fileNamePattern = "CRM_CI_\\d{8}_\\d{6}_\\d{8}\\.zip";
String oldProvIdPattern = "CRM_CI_\\d{8}_\\d{6}_(\\d{8})\\.zip";

String pathToZips = "/Users/John.Smith/Desktop/Test_script/"
String destinationPath = "/Users/John.Smith/Desktop/Test_script/test";

File dir = new File(".");
FileFilter fileFilter = new RegexFileFilter(fileNamePattern);
File[] files = pathToZips.listFiles(fileFilter);
for (File file : files) {
//Handle each zip here
String zipPath = file.getAbsolutePath();


try {
unzipper.unzip(zipPath, destinationPath);
} catch (Exception ex) {
System.out.println("ERROR:Unzip did not work");
}

Pattern pattern = Pattern.compile(oldProvIdPattern);
Matcher matcher = pattern.matcher(file.getName());
if (matcher.find()){
String old_prov_id = matcher.group(1);
System.out.println("Old provider ID :"+old_prov_id );

// add +1 to provider ID
int new_provider_ID = Integer.parseInt(old_prov_id);
new_provider_ID++;
System.out.println("New provider ID :"+new_provider_ID );

// convert provider-id INT into String

String str_provider_id = Integer.toString(new_provider_ID);
System.out.println("New String provider ID :"+str_provider_id );

// concatenate two String into one
StringBuilder bufferPDF = new StringBuilder()
.append(new_provider_ID).append(".pdf");
System.out.println(bufferPDF.toString());

StringBuilder bufferXML = new StringBuilder()
.append(new_provider_ID).append(".xml");
System.out.println(bufferXML.toString());

// convert names of XML and PDF

Path sourcePDF = Paths.get("/Users/John.Smith/Desktop/Test_script/test/" + old_prov_id + ".pdf");
try {
Files.move(sourcePDF, sourcePDF.resolveSibling(bufferPDF.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Path sourceXML = Paths.get("/Users/John.Smith/Desktop/Test_script/test/" + old_prov_id + ".xml");
try {
Files.move(sourceXML, sourceXML.resolveSibling(bufferXML.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// change provider-id and filename in xml file

try {

String filepath = "/Users/John.Smith/Desktop/Test_script/test/" + old_prov_id + ".xml";

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);

// Get the root element provider-id
Node provider = doc.getElementsByTagName("provider-id").item(0);
provider.setTextContent(str_provider_id);

// Get the root element filename
Node filename = doc.getElementsByTagName("filename").item(0);
filename.setTextContent(str_provider_id);


// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);

System.out.println("Done");

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}

}
}

关于java - 如何连接从路径到目录的字符串和目录中的多个 zip 文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40212771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com