导航:首页 > 文件处理 > java读取压缩文件

java读取压缩文件

发布时间:2022-04-14 04:09:51

java怎么读取Zip和RAR里面的文件啊

ZipInputStream是一个指向ZIP文件的流,这个流最重要的方法就是getNextEntry方法,一个zip文件可以包含好几个被压缩的文件,这个方法的功能就是返回下一个目录项,也就是返回zip文件中的下一项,并且把流指向这个目录文件项。getNextEntry的返回值是ZipEntry,它表示zip文件中的一个项,它可以返回这个文件项的大小、名称等。你可以根据它返回的文件大小调用ZipInputStream的read方法来读取需要的字节。给你一个例子:public class ZipTest {
public static void main(String args[]) throws FileNotFoundException, IOException{
ZipInputStream zis = new ZipInputStream(new FileInputStream ("c://a.zip"));//生成读取ZIP文件的流
ZipEntry ze = zis.getNextEntry();//取得下一个文件项
long size = ze.getSize();//取得这一项的大小
FileOutputStream fos = new FileOutputStream("c://"+ze.getName());//产生输出文件对象
for(int i= 0;i<size;i++){//循环读取文件并写入输出文件对象
byte c = (byte)zis.read();
fos.write(c);
}
fos.close();
zis.close();
}
}

㈡ 如何用java读取客户端上传的rar文件

直接通过工具类进行解压或者压缩文件即可。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;

/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}

/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}

/**
* 解压Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration<? extends ZipEntry> entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}

/**
* 将输入流中的数据写到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) > 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}

/**
* 安全关闭多个流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}

/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}

㈢ java中读取压缩包中的文件怎么不用通过解压

你好,我不知道你说的dzp是什么格式文件,但如果是zip的压缩文件,可以看下我的这段代码

ZipFile file = new ZipFile("d:\\1.zip");
ZipEntry entry = file.getEntry("1.xml"); //假如压缩包里的文件名是1.xml
InputStream in=file.getInputStream(entry);
最后就是按照java中一贯的流的处理方式即可

㈣ 如何用java读取zip文件名和zip内文件的文件名,在线等

public static void te(File f) throws IOException {
if (!f.exists() || !f.isDirectory()) {
return;
}
File[] subFiles = f.listFiles();
ZipFile zipFile = null;
for (int i = 0, ii = subFiles == null ? 0 : subFiles.length; i < ii; i++) {
if (subFiles[i].isFile()) {
try {
zipFile = new ZipFile(subFiles[i]);
Enumeration entries = zipFile.entries();
System.out.println("压缩文件:" + subFiles[i].getAbsolutePath());
while(entries.hasMoreElements())
{
System.out.println(" entry:" + ((ZipEntry)entries.nextElement()).getName());
}
zipFile.close();
} catch (ZipException e) {
//System.out.println(e.getMessage());
}
}
}
}

㈤ Java获取压缩包文件列表

public static void main (String args[]) throws IOException {
ZipFile zf = new ZipFile("E:\\test.zip");
InputStream in = new BufferedInputStream(new FileInputStream("E:\\test.zip"));
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();
}

㈥ 怎么从压缩文件中读取文件并显示文件中的内容 java编写

思路是这样的:
1、将压缩文件解压缩到临时目录
2、读取临时目录中的文件或者文件夹(如果是文件夹则读取文件夹中的文件,以此类推)
3、将读取的内容显示
4、删除临时文件夹中的文件或者文件夹

这些都没有难点,你觉得对你来说难度在哪里呢

㈦ 怎么通过url用java直接读取zip类型的文件

最简单的方法。用RadioButton控件就行了。何苦搞得这么累呢。如果真要这样做也可以就是五个控件共用一个事件同时订阅相同事件然后再判断你选中的Checkbox其他的为假代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

㈧ java读取压缩文件并压缩

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class JZip {

public static int iCompressLevel;//压缩比 取值范围为0~9

public static boolean bOverWrite;//是否覆盖同名文件 取值范围为True和False

@SuppressWarnings("unchecked")
private static ArrayList AllFiles = new ArrayList();

public static String sErrorMessage;

private String zipFilePath;

public List<File> srcMap;

public JZip () {

iCompressLevel = 9;
// bOverWrite=true;
}

public JZip(String zipFilePath) throws FileNotFoundException, IOException {

this.zipFilePath = zipFilePath;

}

@SuppressWarnings("unchecked")
public static ArrayList extract (String sZipPathFile , String sDestPath) {

ArrayList AllFileName = new ArrayList();
try {

//先指定压缩档的位置和档名,建立FileInputStream对象
FileInputStream fins = new FileInputStream(sZipPathFile);
//将fins传入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
AllFileName.add(zfile.getAbsolutePath());
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i);
zins.closeEntry();
fouts.close();
}
}
fins.close();
zins.close();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
AllFiles.clear();
return AllFileName;
}

@SuppressWarnings({ "unchecked", "static-access" })
public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {

try {
String sPath;
//先指定压缩档的位置及档名,建立一个FileOutputStream
FileOutputStream fos = new FileOutputStream(sZipPathFile);
//建立ZipOutputStream并将fos传入
ZipOutputStream zos = new ZipOutputStream(fos);
//设置压缩比
zos.setLevel(iCompressLevel);
if (bIsPath == true) {
searchFiles(sPathFile);
sPath = sPathFile;
} else {
File myfile = new File(sPathFile);
sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) + 1);
AllFiles.add(myfile);
}
Object[] myobject = AllFiles.toArray();
ZipEntry ze = null;
//每个档案要压缩,都要透过ZipEntry来处理

FileInputStream fis = null;
BufferedReader in = null;
//byte[] ch = new byte[256];
for (int i = 0 ; i < myobject.length ; i++) {
File myfile = (File) myobject[i];
if (myfile.isFile()) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(myfile.getPath()),"iso8859-1"));
//以档案的名字当Entry,也可以自己再加上额外的路径
//例如ze=new ZipEntry("test\\"+myfiles[i].getName());
//如此压缩档内的每个档案都会加test这个路径
ze = new ZipEntry(myfile.getPath().substring((sPath).length()));
//将ZipEntry透过ZipOutputStream的putNextEntry的方式送进去处理
fis = new FileInputStream(myfile);
zos.putNextEntry(ze);
int len = 0;
//开始将原始档案读进ZipOutputStream
while ((len = in.read()) != -1) {
zos.write(len);
}
fis.close();
zos.closeEntry();
}
}
zos.close();
fos.close();
AllFiles.clear();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Compress error:" + e.getMessage());
sErrorMessage = e.getMessage();
}

}

/*
这是一个递归过程,功能是检索出所有的文件名称
dirstr:目录名称
*/
@SuppressWarnings("unchecked")
private static void searchFiles (String dirstr) {

File tempdir = new File(dirstr);
if (tempdir.exists()) {
if (tempdir.isDirectory()) {
File[] tempfiles = tempdir.listFiles();
for (int i = 0 ; i < tempfiles.length ; i++) {
if (tempfiles[i].isDirectory())
searchFiles(tempfiles[i].getPath());
else {
AllFiles.add(tempfiles[i]);
}
}
} else {
AllFiles.add(tempdir);
}
}
}
public String getZipFilePath() {
return zipFilePath;
}
public void setZipFilePath(String zipFilePath) {
this.zipFilePath = zipFilePath;
}

/**
* 解析zip文件得到文件名
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean parserZip() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(zipFilePath);

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

ZipEntry entry;

try {

srcMap = new ArrayList<File>();

while ((entry = zis.getNextEntry()) != null) {

File file = new File(zipFilePath + File.separator + entry.getName());
srcMap.add(file);

}

zis.close();

fis.close();

return true;

} catch (IOException e) {

return false;

}

}

/**
*
* @param zipFileName 待解压缩的ZIP文件
* @param extPlace 解压后的文件夹
*/
public static void extZipFileList(String zipFileName, String extPlace) {
try {

ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));
File files = new File(extPlace);
files.mkdirs();

ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(files + entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
} else {
OutputStream os = new FileOutputStream(files+File.separator + entryName);

// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();
System.out.println("解压文件:" + entryName);
}
}
} catch (IOException e) {
}
}

@SuppressWarnings("static-access")
public static void main(String args[]){

}

}

㈨ java如何读取压缩包中的文本文件

压缩包的里的文件不能直接读取,只能先解压缩,再读取。
建议:可以用apache的工具类,先解压缩成临时文件,再读取,最后删除临时文件。

阅读全文

与java读取压缩文件相关的资料

热点内容
linuxredis30 浏览:541
狸窝pdf转换器 浏览:696
ajax调用java后台 浏览:904
活塞式压缩机常见故障 浏览:614
break算法 浏览:731
换电池的app是什么 浏览:771
单片机ad采样快速发送电脑 浏览:22
第五人格服务器错误是什么回事儿 浏览:467
查看手机谷歌服务器地址 浏览:191
python操作zookeeper 浏览:705
苹果手机dcim文件夹显示不出来 浏览:430
如何压缩文件夹联想电脑 浏览:583
程序员的学习之旅 浏览:440
apkdb反编译 浏览:922
雪花算法为什么要二进制 浏览:825
在文档中打开命令行工具 浏览:608
android图标尺寸规范 浏览:369
python实用工具 浏览:208
流量计pdf 浏览:936
科东加密认证价格 浏览:532