java解压压缩文件
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
/**
* 获得zip文件里的所有文件
* @author Administrator
*
*/
public class ZipFile {
public ZipFile() throws IOException
{
java.util.zip.ZipFile zf = new java.util.zip.ZipFile("E:/Java/Project.zip");
Enumeration e = zf.entries();
while(e.hasMoreElements())
{
ZipEntry ze = (ZipEntry) e.nextElement();
if(!ze.isDirectory())
System.out.println(new String(ze.getName().getBytes("ISO-8859-1"), "GB2312"));
}
}
public static void main(String[] args) {
try {
new ZipFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. java 解压文件
给你找了一个 你参考一下吧:
package com.da.unzip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Unzip {
public static void main(String[] args) throws Exception {
Unzip unzip = new Unzip();
String zippath = "C:\\unzip\\";// /解压到的目标文件路径
String zipDir = "C:\\data\\";// 要解压的压缩文件的存放路径
File file = new File(zipDir);
List list = unzip.getSubFiles(file);
for (Object obj : list) {
String realname = ((File)obj).getName();
System.out.println(realname);
int end = realname.lastIndexOf(".");
System.out.println("要解压缩的文件名.........."+zipDir+realname);
System.out.println("解压到的目录" +zippath+realname.substring(0, end));
unzip.testReadZip(zippath,zipDir+realname);
}
}
/*
* 解压缩功能. 将zippath目录文件解压到unzipPath目录下. @throws Exception
*/
public void ReadZip(String zippath, String unzipPath) throws Exception {
ZipFile zfile = new ZipFile(unzipPath);// 生成一个zip文件对象
System.out.println(zfile.getName());// 获取要解压的zip的文件名全路径
Enumeration zList = zfile.entries();// 返回枚举对象
ZipEntry ze = null;// 用于表示 ZIP 文件条目
byte[] buf = new byte[1024];// 声明字节数组
/**
* 循环获取zip文件中的每一个文件
*/
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory())// 如果为目录条目,则返回 true,执行下列语句
{
System.out.println("Dir: " + ze.getName() + " skipped..");
continue;
}
int begin = zfile.getName().lastIndexOf("\\") + 1;
int end = zfile.getName().lastIndexOf(".");
String zipRealName = zfile.getName().substring(begin, end);
System.out.println("解压缩开始Extracting:"+ze.getName()+"\t"+ze.getSize()+"\t"+ze.getCompressedSize());
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中,并加上缓冲
OutputStream os = new BufferedOutputStream(
new FileOutputStream(getRealFileName(zippath + "\\"
+ zipRealName, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
String fileName = getRealFileName(zippath, ze.getName()).getName();
System.out.println("解压出的文件名称:" + fileName);
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
// System.out.println("解压缩结束Extracted: "+ze.getName());
}
zfile.close();
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param zippath
* 指定根目录
* @param absFileName
* 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private File getRealFileName(String zippath, String absFileName) {
String[] dirs = absFileName.split("/", absFileName.length());
File ret = new File(zippath);// 创建文件对象
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);
}
}
if (!ret.exists()) {// 检测文件是否存在
ret.mkdirs();// 创建此抽象路径名指定的目录
}
ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
// 路径名字符串创建一个新 File 实例
return ret;
}
}
3. 怎样用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();
}
}
}
4. 如何在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
5. 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);
}
6. 安装java解压缩核心文件失败怎么办
java压缩文件解压失败
java压缩文件解压失败_java安装 解压缩核心文件失败
第一步:下载 JDK
从 SUN 网站下载 JDK6 或以上版本,这里以 jdk-6u2-windows-i589-p 版为例。
第二步:安装 JDK
(1):双击 jdk-6u2-windows-i589-p.exe 文件,我们这里安装路径为:D:\common\Java
(2):安装完成过后,JDK 文件夹包括:
D:\common\Java\jdk1.6.0_02:是 JDK 的安装路径;
bin:binary 的简写,下面存放的是 Java 的各种可执行文件;
db:JDK6 新加入的 Apache 的 Derby 数据库,支持 JDBC4.0 的规范;
include:需要引入的一些头文件,主要是 c 和 c++的,JDK 本身是通过 C 和 C++实现的;
jre:Java 运行环境;
lib:library 的简写,JDK 所需要的一些资源文件和资源包。
第三步:配置环境变量
安装完成后,还要进行 Java 环境的配置,才能正常使用,步骤如下:
(1):在我的电脑点击右键——〉选择属性,
(2):在弹出界面上:选择高级——〉环境变量,
(3):在系统变量里面找到“Path”这一项,然后双击它,在弹出的界面上,在变量值开头添加如下语句“D:\common\Java\jdk1.6.0_02\bin;”,注意不要忘了后面的分号,
(4):然后点击编辑系统变量界面的确定按钮,然后点击环境变量界面的“新建”,
(5):在上面填写变量名为:JAVA_HOME,变量值为:D:\common\Java\jdk1.6.0_02;,注意分号。
(6):然后点击新建系统变量界面的确定按钮,然后点击环境变量界面的“新建”,弹出新建系统变量界面,在上面填写变量名为:classpath ,变量值为:.; ,注意是点和分号。
(7):然后点击一路点击确定按钮,到此设置就完成了。
那么为何要设置这些环境变量呢,如何设置呢:
PATH:提供给操作系统寻找到 Java 命令工具的路径。通常是配置到 JDK 安装路径\bin,如:D:\common\Java\jdk1.6.0_02\bin;。
JAVA_HOME:提供给其它基于 Java 的程序使用,让它们能够找到 JDK 的位置。通常配置到 JDK 安装路径,如:D:\common\Java\jdk1.6.0_02;。注意:JAVA_HOME必须书写正确,全部大写,中间用下划线。
CLASSPATH:提供程序在运行期寻找所需资源的路径,比如:类、文件、图片等等。
注意:在 windows 操作系统上,最好在 classpath 的配置里面,始终在前面保持“.;”的配置,在 windows 里面“.”表示当前路径。
第四步:检测安装配置是否成功
进行完上面的步骤,基本的安装和配置就好了,怎么知道安装成功没有呢?
点击开始——〉点击运行,在弹出的对话框中输入“cmd”,然后点击确定,在弹出的 dos 窗口里面,输入“javac”,然后回车,出现如下界面则表示安装配置成功。
好了,现在 Java 的开发环境就配置好了,接下来就可以进入java的第一个程序了。
7. 怎样用JAVA解压winrar加密的zip包(不要调用winrar的命令)
WinRAR <命令> -<参数1> -<参数N> <压缩包> <文件...> <@列表文件...> <解压缩路径\>
命令 要 WinRAR 运行的字符组合代表功能
参数 切换操作指定类型,压缩强度,压缩包类型,等等的定义。
压缩包 要进行的压缩包名。
文件 要进行的文件名。
列表文件 列表文件是包含要处理文件名称的纯文本。文件名应该在第一卷启动。可以在列表文件中使用//字符后添加注释。例如,你可以包含两列字符串创建 backup.lst: c:\work\doc\*.txt //备份文本文档 c:\work\image\*.bmp //备份图片 c:\work\misc 并接着运行: rar a backup @backup.lst 你可以在命令行中同时指定普通的文件名和列表文件名。
解压缩路径 只与命令 e 和 x ,搭配使用。指出解压缩文件添加的位置。如果文件夹不存在时,会自动创建。
注意事项
a) 如果未指定 文件 或是 列表文件 时,WinRAR 将会以缺省的 *.* 运行全部的文件;
b) 如果未指定压缩包扩展名时,WinRAR 将会使用在 压缩配置 中选定的默认压缩格式。但你可以指定 .RAR 或 .ZIP 扩展名来替换它们;
c) 在命令行所输入的参数会替换相同的配置设置值;
d) 在命令 c、e、s、t、rr、k 和 x 可在压缩包名中使用通配符。如此可以用单一的命令来进行超过一个以上的压缩包,除此之外,如果你指定 -r 参数于这些命令时,它们将会搜索在子文件夹中的压缩包;
e) 某些命令和参数只应用在 RAR 压缩包,有些则在 RAR 和 ZIP 都可使用,而某些则可应用在全部的压缩格式。这一些都得看压缩格式所提供的特性而定;
f) 命令和参数的大小写是相同意思的,你可以用大写或者小写来下命令均可
8. java 中的war格式的压缩包怎么解压
你好,这些是打包好的部署包,将这些直接丢如Tomcat WebApp目录下就可以通过Web访问了,如果你想看源码,用解压缩软件都可以的,就看这包里面有没有源码了,zip ,winRAR ,7-zip都可以解压出来,如果想看源码,没有的话,找个反编译的软件把class文件拖进去就可以看到了..jd-gui 这个可以,网上找找
9. java中怎么解压rar文件 到指定文件目录中
1.代码如下:
[java] view plain
<span style="font-size:18px;background-color: rgb(204, 204, 204);">package cn.gov.csrc.base.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 将文件夹下面的文件
* 打包成zip压缩文件
*
* @author admin
*
*/
public final class FileToZip {
private FileToZip(){}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");
}else{
try {
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");
}else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}
public static void main(String[] args){
String sourceFilePath = "D:\\TestFile";
String zipFilePath = "D:\\tmp";
String fileName = "12700153file";
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失败!");
}
}
}
</span>
2.结果如下:
文件打包成功!
3.到D:/tmp下查看,你会发现生成了一个zip压缩包.
10. javazip压缩包过大解压失败
javazip压缩包过大解压失败的原因:网络传输不好导致文件下载损坏、网站提供的RAR压缩包最初被损坏、使用的下载工具不够完善。我们可以通过压缩软件里的“修复压缩文件”解决javazip压缩包过大解压失败的问题。