java - 如何从 zip 中读取不同的 xml 文件
问题描述
我的问题是如何从 zip 中读取文件数据,但实际上,我已经实现了该部分,但问题是我在 zip 中有不同的文件(所有 xml)我不想使用 xml 解析器解析这些文件无论如何我只是想阅读它,目前我的代码正在阅读所有 xml 文件,并且我将所有数据附加到一个字符串中,如何分别读取和存储不同的文件数据。
我的代码-
public class unzipFile {
public static void main(String[] args) throws IOException {
String zipFileName = "people.zip";
StringBuilder s = new StringBuilder();
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry zipEntry;
int read;
while ((zipEntry = zis.getNextEntry())!= null) {
while ((read = zis.read(buffer, 0, 1024)) >= 0) {
s.append(new String(buffer, 0, read));
}
}
while (zipEntry != null){
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Unzip complete");
System.out.println("S = "+s);
}
它将一次性打印所有文件中的所有数据,我需要更改什么才能分别读取不同文件的数据?任何人请帮助我谢谢!
解决方案
这会将所有文件打印到out
. 您可以更改while
块以适应您的实际需要。
public class unzipFile {
public static void main(String[] args) throws IOException {
String zipFileName = "people.zip";
StringBuilder s = new StringBuilder();
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry zipEntry;
int read;
while ((zipEntry = zis.getNextEntry())!= null) {
System.out.println("File = "+zipEntry.getName());
while ((read = zis.read(buffer, 0, 1024)) >= 0) {
s.append(new String(buffer, 0, read));
}
System.out.println("S = "+s);
s.clear();
}
zis.closeEntry();
zis.close();
System.out.println("Unzip complete");
}
推荐阅读
- java - Java 流可选,对两个项目进行空检查
- python - HoloViews 超出了 IOPub 消息速率
- maven - 如何激活 Maven 配置文件使用环境变量?
- windows - 如何在 Host.UI.PromptForCredential Powershell Windows 10 中输入密码文本字符串
- tabs - 当我尝试将我的自定义应用程序添加到 Micrsoft TEAMS 频道中作为新 TAB 时,保存按钮未显示
- android-studio - 设备(小马 Poco f1)未通过 USB 检测 Android Studio
- r - 从R中的文本文件中提取数字
- java - 如何使用 OpenTok 收听来自我的应用程序的传入视频通话?
- amazon-ec2 - 如何通过 Ansible 在 AWS EC2 实例上部署 Consul
- ruby - (Friendly_id) 如何在嵌套路由中使用 slug 而不是 id?