Java读取zip文件内的文件内容

package com.zip;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class UnZip {

	public static void main(String[] args) throws Exception {
		try {
			readZipFile("D:\\file_all\\总署OA改造\\书生改造\\书生下载\\c154b22e-3320-992f-438c-3fb131ab3130.zip");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void readZipFile(String file) throws Exception {
		ZipFile zf = new ZipFile(file);
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		ZipInputStream zin = new ZipInputStream(in);
		ZipEntry ze;
		while ((ze = zin.getNextEntry()) != null) {
			if (ze.isDirectory()) {
			} else {
				System.err.println("file - " + ze.getName() + " : "
						+ ze.getSize() + " bytes");
				long size = ze.getSize();
				if (size > 0) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(zf.getInputStream(ze)));
					String line;
					while ((line = br.readLine()) != null) {
						System.out.println(line);
					}
					br.close();
				}
				System.out.println();
			}
		}
		zin.closeEntry();
		zf.close();
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Javajava.util.zip包来实现解压zip文件读取内容。下面是一个示例代码: ```java import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipExample { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String destinationFolder = "path/to/destination/folder"; try { unzip(zipFilePath, destinationFolder); readContents(destinationFolder); } catch (IOException e) { e.printStackTrace(); } } public static void unzip(String zipFilePath, String destinationFolder) throws IOException { byte[] buffer = new byte[1024]; File folder = new File(destinationFolder); if (!folder.exists()) { folder.mkdirs(); } try (ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath)))) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String entryName = zipEntry.getName(); File entryFile = new File(destinationFolder + File.separator + entryName); if (entryFile.getParentFile() != null && !entryFile.getParentFile().exists()) { entryFile.getParentFile().mkdirs(); } if (!zipEntry.isDirectory()) { int bytesRead; while ((bytesRead = zipInputStream.read(buffer)) != -1) { entryFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(entryFile); fileOutputStream.write(buffer, 0, bytesRead); fileOutputStream.close(); } } else { entryFile.mkdirs(); } zipInputStream.closeEntry(); } } } public static void readContents(String folderPath) { File folder = new File(folderPath); if (folder.exists() && folder.isDirectory()) { File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { System.out.println("Directory: " + file.getName()); } else { System.out.println("File: " + file.getName()); } } } } } } ``` 在上述示例代码中,你需要替换`zipFilePath`和`destinationFolder`变量的值为你实际的zip文件路径和解压后的目标文件夹路径。`unzip`方法负责解压zip文件,`readContents`方法用于读取解压后的文件内容打印。 请确保你已经正确配置了Java环境,并导入了必要的类和包。运行示例代码后,它将解压zip文件打印出解压后的文件文件夹列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值