当前位置:首页 » 文件管理 » javazip压缩解压

javazip压缩解压

发布时间: 2022-10-18 07:09:53

1. java解压zip文件

不好意思搞反了,这样就更简单了。
用这个构造方法ZipInputStream(InputStream in);接收传过来的流,然后用这个类的getNextEntry()方法解压缩文件,最后调用read(byte[] b, int off, int len)方法将数据写入byte数组。
ZipInputStream zin = new ZipInputStream(in);
ZipEntry entry = null;
while((entry=zin.getNextEntry())!=null){
if(entry.isDirectory()||entry.getName().equals("..\\"))
continue;
BufferedInputStream bin = new BufferedInputStream(zin);
byte[] buf = new byte[];
bin.read(buf,0,1);
}

2. 怎样用java快速实现zip文件的压缩解压缩

packagezip;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.util.Enumeration;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedOutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
importorg.apache.commons.lang3.StringUtils;
publicclassZipUtil{

/**
*递归压缩文件夹
*@paramsrcRootDir压缩文件夹根目录的子路径
*@paramfile当前递归压缩的文件或目录对象
*@paramzos压缩文件存储对象
*@throwsException
*/
privatestaticvoidzip(StringsrcRootDir,Filefile,ZipOutputStreamzos)throwsException
{
if(file==null)
{
return;
}

//如果是文件,则直接压缩该文件
if(file.isFile())
{
intcount,bufferLen=1024;
bytedata[]=newbyte[bufferLen];

//获取文件相对于压缩文件夹根目录的子路径
StringsubPath=file.getAbsolutePath();
intindex=subPath.indexOf(srcRootDir);
if(index!=-1)
{
subPath=subPath.substring(srcRootDir.length()+File.separator.length());
}
ZipEntryentry=newZipEntry(subPath);
zos.putNextEntry(entry);
BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file));
while((count=bis.read(data,0,bufferLen))!=-1)
{
zos.write(data,0,count);
}
bis.close();
zos.closeEntry();
}
//如果是目录,则压缩整个目录
else
{
//压缩目录中的文件或子目录
File[]childFileList=file.listFiles();
for(intn=0;n<childFileList.length;n++)
{
childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());
zip(srcRootDir,childFileList[n],zos);
}
}
}

/**
*对文件或文件目录进行压缩
*@paramsrcPath要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
*@paramzipPath压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹
*@paramzipFileName压缩文件名
*@throwsException
*/
publicstaticvoidzip(StringsrcPath,StringzipPath,StringzipFileName)throwsException
{
if(StringUtils.isEmpty(srcPath)||StringUtils.isEmpty(zipPath)||StringUtils.isEmpty(zipFileName))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
CheckedOutputStreamcos=null;
ZipOutputStreamzos=null;
try
{
FilesrcFile=newFile(srcPath);

//判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生)
if(srcFile.isDirectory()&&zipPath.indexOf(srcPath)!=-1)
{
thrownewParameterException(ICommonResultCode.INVALID_PARAMETER,".");
}

//判断压缩文件保存的路径是否存在,如果不存在,则创建目录
FilezipDir=newFile(zipPath);
if(!zipDir.exists()||!zipDir.isDirectory())
{
zipDir.mkdirs();
}

//创建压缩文件保存的文件对象
StringzipFilePath=zipPath+File.separator+zipFileName;
FilezipFile=newFile(zipFilePath);
if(zipFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
=newSecurityManager();
securityManager.checkDelete(zipFilePath);
//删除已存在的目标文件
zipFile.delete();
}

cos=newCheckedOutputStream(newFileOutputStream(zipFile),newCRC32());
zos=newZipOutputStream(cos);

//如果只是压缩一个文件,则需要截取该文件的父目录
StringsrcRootDir=srcPath;
if(srcFile.isFile())
{
intindex=srcPath.lastIndexOf(File.separator);
if(index!=-1)
{
srcRootDir=srcPath.substring(0,index);
}
}
//调用递归压缩方法进行目录或文件压缩
zip(srcRootDir,srcFile,zos);
zos.flush();
}
catch(Exceptione)
{
throwe;
}
finally
{
try
{
if(zos!=null)
{
zos.close();
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}

/**
*解压缩zip包
*@paramzipFilePathzip文件的全路径
*@paramunzipFilePath解压后的文件保存的路径
*@paramincludeZipFileName解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含
*/
@SuppressWarnings("unchecked")
publicstaticvoinzip(StringzipFilePath,StringunzipFilePath,booleanincludeZipFileName)throwsException
{
if(StringUtils.isEmpty(zipFilePath)||StringUtils.isEmpty(unzipFilePath))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
FilezipFile=newFile(zipFilePath);
//如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径
if(includeZipFileName)
{
StringfileName=zipFile.getName();
if(StringUtils.isNotEmpty(fileName))
{
fileName=fileName.substring(0,fileName.lastIndexOf("."));
}
unzipFilePath=unzipFilePath+File.separator+fileName;
}
//创建解压缩文件保存的路径
FileunzipFileDir=newFile(unzipFilePath);
if(!unzipFileDir.exists()||!unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}

//开始解压
ZipEntryentry=null;
StringentryFilePath=null,entryDirPath=null;
FileentryFile=null,entryDir=null;
intindex=0,count=0,bufferSize=1024;
byte[]buffer=newbyte[bufferSize];
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
ZipFilezip=newZipFile(zipFile);
Enumeration<ZipEntry>entries=(Enumeration<ZipEntry>)zip.entries();
//循环对压缩包里的每一个文件进行解压
while(entries.hasMoreElements())
{
entry=entries.nextElement();
//构建压缩包中一个文件解压后保存的文件全路径
entryFilePath=unzipFilePath+File.separator+entry.getName();
//构建解压后保存的文件夹路径
index=entryFilePath.lastIndexOf(File.separator);
if(index!=-1)
{
entryDirPath=entryFilePath.substring(0,index);
}
else
{
entryDirPath="";
}
entryDir=newFile(entryDirPath);
//如果文件夹路径不存在,则创建文件夹
if(!entryDir.exists()||!entryDir.isDirectory())
{
entryDir.mkdirs();
}

//创建解压文件
entryFile=newFile(entryFilePath);
if(entryFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
=newSecurityManager();
securityManager.checkDelete(entryFilePath);
//删除已存在的目标文件
entryFile.delete();
}

//写入文件
bos=newBufferedOutputStream(newFileOutputStream(entryFile));
bis=newBufferedInputStream(zip.getInputStream(entry));
while((count=bis.read(buffer,0,bufferSize))!=-1)
{
bos.write(buffer,0,count);
}
bos.flush();
bos.close();
}
}

publicstaticvoidmain(String[]args)
{
StringzipPath="d:\ziptest\zipPath";
Stringdir="d:\ziptest\rawfiles";
StringzipFileName="test.zip";
try
{
zip(dir,zipPath,zipFileName);
}
catch(Exceptione)
{
e.printStackTrace();
}

StringzipFilePath="D:\ziptest\zipPath\test.zip";
StringunzipFilePath="D:\ziptest\zipPath";
try
{
unzip(zipFilePath,unzipFilePath,true);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}

3. java中怎么用cmd命令解压zip文件

对于zip文件,java有自带类库java.util.zip;可是要想解压rar文件只能靠第三方类库,我试过两个:com.github.junrar和de.innosystec.unrar,前者解压时可能会出现crcError,后者pom配置时报错;利用cmd命令调用winRAR进行解压,无疑方便快捷很多。
调用cmd命令
public
static
boolean
exe(String
cmd)
{
Runtime
runtime
=
Runtime.getRuntime();try
{
Process
p
=
runtime.exec(cmd);
BufferedReader
reader
=
new
BufferedReader(new
InputStreamReader(p.getInputStream(),"GBK"));
String
line
=
reader.readLine();

while(line!=null)
{
logger.info(line);
line
=
reader.readLine();
}
reader.close();

if(p.waitFor()!=0)
{return
false;
}
}
catch
(IOException
e)
{

//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(InterruptedException
e)
{

//
TODO
Auto-generated
catch
block
e.printStackTrace();
}return
true;
}
首先利用runtime.exec()执行指令,得到process,从process.getInputStream()中获取回显字符并打印,打印回显时可能会出现中文乱码,这个和操作系统编码有关,我这里是GBK编码,所以在new
inputstreamReader时加入了编码参数”GBK“
命令行字符串
如果需要调用cmd命令,如cd等,可写”cmd
c
cd
目录”。对于直接调用exe执行,则可以写成”exe文件绝对路径
参数”,在命令行字符串中,含有空格的路径或者字符串应该再加上引号,即””exe文件绝对路径”
”参数”“
winRAR调用
我这里安装目录是C:/Program
Files/WinRAR,将D:1.rar
解压到D:,则写成””C:/Program
Files/WinRAR/unRar.exe”
x
-y
D:/1.rar
D:/”,x代表绝对路径解压,-y表示全部确定;压缩的命令如下:“”C:/Program
Files/WinRAR/rar.exe”
a
-ep1
D:2.rar
D:源目录”,a表示添加文件到压缩文件,-ep1表示排除基本目录,如D:winrar ar这个目录,如果没有-ep1那么压缩包中会出现winrar目录路径,而加了之后就只将当前目录打包,只有rar目录

4. 如何在java中实现对zip和rar文件的解压

java中有zip包,可以使用

publicvoidgetZipFiles(StringzipFile,StringdestFolder)throwsIOException{
BufferedOutputStreamdest=null;
ZipInputStreamzis=newZipInputStream(
newBufferedInputStream(
newFileInputStream(zipFile)));
ZipEntryentry;
while((entry=zis.getNextEntry())!=null){
System.out.println("Extracting:"+entry.getName());
intcount;
bytedata[]=newbyte[BUFFER];

if(entry.isDirectory()){
newFile(destFolder+"/"+entry.getName()).mkdirs();
continue;
}else{
intdi=entry.getName().lastIndexOf('/');
if(di!=-1){
newFile(destFolder+"/"+entry.getName()
.substring(0,di)).mkdirs();
}
}
FileOutputStreamfos=newFileOutputStream(destFolder+"/"
+entry.getName());
dest=newBufferedOutputStream(fos);
while((count=zis.read(data))!=-1)
dest.write(data,0,count);
dest.flush();
dest.close();
}
}

rar的只能用第三方api,比如junrar

https://github.com/edmund-wagner/junrar

5. 如何在java中解压zip和rar文件

java中有zip包,可以使用

public void getZipFiles(String zipFile, String destFolder) throws IOException {
BufferedOutputStream dest = null;
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(zipFile)));
ZipEntry entry;
while (( entry = zis.getNextEntry() ) != null) {
System.out.println( "Extracting: " + entry.getName() );
int count;
byte data[] = new byte[BUFFER];

if (entry.isDirectory()) {
new File( destFolder + "/" + entry.getName() ).mkdirs();
continue;
} else {
int di = entry.getName().lastIndexOf( '/' );
if (di != -1) {
new File( destFolder + "/" + entry.getName()
.substring( 0, di ) ).mkdirs();
}
}
FileOutputStream fos = new FileOutputStream( destFolder + "/"
+ entry.getName() );
dest = new BufferedOutputStream( fos );
while (( count = zis.read( data ) ) != -1)
dest.write( data, 0, count );
dest.flush();
dest.close();
}
}

rar的只能用第三方api,比如junrar

6. java zip解压

如果把out.close()写在注释处,那么意味着while循环中创建的out输出流对象没有关闭,要知道,如果这个流没有关闭,那么该流缓冲区中的数据不会被刷新到实际目标文件中。
因此只有最后一个文件有内容(因为out被关闭时指向最后一个文件)。

7. JAVA解压缩ZIP包问题:

我试了一下,没有问题
先问一下,你用的JDK是什么版 本。我是1.6_20,直接用你的程序。
zipFile = new ZipFile(new File(zipfile),"GBK");
Enumeration enumeration = zipFile.getEntries();
是报错的。
我改成了
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.entries();

这应该不是主要问题。

有没有可能是你的压缩包损坏了。或是包里的那个文件坏了,跟一下断点,看一下是解那个文件出的错。

8. 如何解决java程序解压含有中文名的zip压缩包出现乱码

上次利用java自动的java.util.zip.ZipEntry和�0�2java.util.zip.ZipFile来解压zip文件,今天发现程序在读取解压文件时居然报了空指针异常,debug程序后发现时读取不到文件,产生原先是zip压缩文件中含有中文的名称,读取文件名为乱码,
报找不到文件名,所以报了空指针,想到ant构建文件也有这个功能,换了apache的ant.jar居然解决了中文的问题。
备份下。
�0�2import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;/*** 读取zip压缩文件中文本的内容
* @author fish*/public class ReadZip {
public static void main(String args[]) {try {String fileName = "D:/workspace/java/src/ReadZip.zip";
//构造ZipFile
ZipFile zf = new ZipFile(new File(fileName));
//返回 ZIP file entries的枚举.
Enumeration<? extends ZipEntry entries = zf.getEntries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
System.out.println("name:"+ze.getName());
long size = ze.getSize();
if (size 0) {
System.out.println("Length is " + size);
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {

9. java如何解压一个ZIP压缩过的数据

1楼的方法。先解压开后,你再把文件读成流,把数据存数据库

10. java 以流的形式解压带密码的zip

可以使用 Runtime 直接调用 winRar 的命令行命令来解压缩
注意:
1、winRar命令使用,在dos下输入 unrar 就可以看到全部的命令说明。该命令在winRar的安装目录下
2、winRar命令行命令的路径问题,也就是path。要么加入系统变量path中,要么在winRar的安装目录下执行程序
以下是程序代码,解压 test.rar 到当前目录下,密码123

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestRunTime {

public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("unrar e test.rar -p123");//执行解压缩命令
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;

while ((lineStr = inBr.readLine()) != null)
System.out.println(lineStr);
// 检查命令是否执行失败。
if (p.waitFor() != 0) {
if (p.exitValue() == 1)// p.exitValue()==0表示正常结束,1:非正常结束
System.err.println("命令执行失败!");
}

} catch (Exception e) {
e.printStackTrace();
}

}

}

热点内容
编译期错误提示 发布:2024-10-07 20:07:23 浏览:296
阿里云服务器打开慢 发布:2024-10-07 20:06:33 浏览:577
磁存储行业的前景 发布:2024-10-07 20:01:29 浏览:548
android对象序列化 发布:2024-10-07 20:01:28 浏览:760
安卓上面的微信如何恢复 发布:2024-10-07 19:57:45 浏览:52
510低配包括哪些配置 发布:2024-10-07 19:38:21 浏览:556
ping服务器地址一直丢包 发布:2024-10-07 19:37:34 浏览:588
怎么去除手机锁屏密码 发布:2024-10-07 18:56:05 浏览:67
怎样注册手机贴吧账号密码是什么 发布:2024-10-07 18:55:17 浏览:365
端口安全配置属于什么 发布:2024-10-07 18:41:01 浏览:794