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壓縮包過大解壓失敗的問題。