Java不解压直接读取压缩包文件

本文提供了一个使用Java读取ZIP文件的具体示例代码,展示了如何遍历ZIP文件中的所有条目,并读取每个文件的内容。适用于需要处理ZIP归档文件的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比较简单,直接上代码!

package com.wisdom.csmp.tools.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipUtil2
{
	public static void main(String[] args)
	{
		readZipFile("G:\\LogTest_bak\\logs\\core\\data.zip", "UTF-8");
	}

	@SuppressWarnings("rawtypes")
	public static void readZipFile(String file, String encoding)
	{
        try
		{
			ZipFile zf = new ZipFile(file);
			for (Enumeration entries = zf.entries(); entries.hasMoreElements();) 
			{
				ZipEntry ze = (ZipEntry) entries.nextElement();
				try
				{
					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), encoding));
							String line;
							while ((line = br.readLine()) != null)
							{
								System.out.println(line);
							}
							br.close();
						}
						System.out.println();
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
        catch(Exception e)
        {
        	e.printStackTrace();
        }
	}
}


### Java 实现解 7z 压缩包文件 为了使用Java实现解7z压缩包的功能,可以借助`sevenzipjbinding`库。此库提供了对7-Zip功能的支持,允许程序化地处理各种类型的压缩文件而无需依赖外部工具。 #### 添加依赖项 首先,在项目的构建配置中加入SevenZipJBinding的Maven依赖: ```xml <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <artifactId>sevenzipjbinding-all-platforms</artifactId> <version>16.02-2.01-SNAPSHOT</version> </dependency> ``` #### 示例代码:解7z文件到指定目录 下面是一段完整的Java代码片段,展示了如何读取并提取给定路径下的`.7z`文件至目标位置。 ```java import net.sf.sevenzipjbinding.ExtractAskMode; import net.sf.sevenzipjbinding.IInArchive; import net.sf.sevenzipjbinding.SevenZip; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; import java.io.File; import java.io.FileOutputStream; public class SevenZExtractor { public static void extract(String sourceFilePath, String destinationDirPath) throws Exception { try (RandomAccessFileInStream randomAccessFileInputStream = new RandomAccessFileInStream(new File(sourceFilePath))) { IInArchive inArchive = SevenZip.openInArchive(null, randomAccessFileInputStream); int numberOfItems = (int)inArchive.getNumberOfItems(); for(int i=0; i<numberOfItems ;i++){ var item=inArchive.getItem(i); if (!item.isFolder()) { // Skip folders. final long size=item.getSize(); byte[] buffer=new byte[(int)Math.min(size ,8*1024)]; FileOutputStream fos=null; try{ fos=new FileOutputStream(destinationDirPath+"/"+item.getPath()); while(true){ ExtractAskMode mode=inArchive.extractItem(item.getIndex(),buffer,0,buffer.length,fos::write); switch(mode){ case ASK_OVERWRITE: break; case CANT_DECIDE: throw new RuntimeException("Can't decide"); default://ASK_SKIP continue; } } }finally{ if(fos!=null){ fos.close(); } } } } inArchive.close(); } } } ``` 上述代码通过遍历存档内的每一项,并将其内容逐个写出到磁盘上的相应位置完成了解过程[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值