JAVA操作解析zip文件,内存处理zip文件条目

        对zip文件解析,本可以直接解析zip文件成个个子文件,但是有的情况是我们并不需要生成文件(这个动作会保存生成文件),因此这里提供一个内存在处理的方式,如有问题,望指正。

/**
	 * 读取ZIP文件流
	 * @param inputStream
	 * @return
	 * @throws Exception
	 */
	public static  List<byte[]> readZipInputStream(ZipInputStream zis) throws Exception {
		List<byte[]> lst_byte = new ArrayList<byte[]>();
		while (zis.getNextEntry() != null) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = zis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);    
			}
			lst_byte.add(bos.toByteArray());
			bos.close();
			zis.getNextEntry();  //关闭该条目
		}
		return lst_byte;
	}


以上方法是直接通过输入流InputStream得到ZipInputStream,然后调用方法即可。

InputStream inputStream = conn.getInputStream(); //得到输入流  

ZipInputStream zis = new ZipInputStream(inputStream);

对于得到的List<byte[]> 这个每一个byte[]都是一个子文件字节表达,可以直接操作。

比如zip文件中都是文本文件,那么循环List new String(byte[], "UTF-8") 就可以获取到所有文件的字符文本了。




  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个示例代码,演示如何使用Java下载zip文件解析其中的Excel文件,获取数据。 ```java import java.io.*; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class ZipExcelParser { public static void main(String[] args) throws Exception { // Step 1: 下载zip文件 String url = "https://example.com/file.zip"; // zip文件的URL String zipFilePath = "file.zip"; // 下载后的zip文件路径 downloadFile(url, zipFilePath); // Step 2: 解析Excel文件 String excelFilePath = null; // Excel文件路径 try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFilePath))) { ZipEntry entry = zip.getNextEntry(); while (entry != null) { String fileName = entry.getName(); if (fileName.endsWith(".xlsx")) { // 只处理.xlsx文件 excelFilePath = fileName; File file = new File(excelFilePath); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = zip.read(bytes)) >= 0) { fos.write(bytes, 0, length); } fos.close(); break; } entry = zip.getNextEntry(); } } // Step 3: 获取Excel数据 List<List<String>> data = new ArrayList<>(); Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFilePath)); Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个Sheet中 for (Row row : sheet) { List<String> rowData = new ArrayList<>(); for (Cell cell : row) { rowData.add(cell.toString()); } data.add(rowData); } workbook.close(); // 打印数据 for (List<String> rowData : data) { System.out.println(String.join(",", rowData)); } } // 下载文件 private static void downloadFile(String url, String filePath) throws Exception { BufferedInputStream in = new BufferedInputStream(new URL(url).openStream()); FileOutputStream fos = new FileOutputStream(filePath); byte[] dataBuffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { fos.write(dataBuffer, 0, bytesRead); } fos.close(); in.close(); } } ``` 请注意,此示例代码使用了Apache POI库来解析Excel文件。如果您尚未安装它,请在项目中添加以下依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值