導航:首頁 > 文件處理 > java中的壓縮包怎麼解壓

java中的壓縮包怎麼解壓

發布時間:2023-02-07 18:06:23

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);
}

㈡ java 中的war格式的壓縮包怎麼解壓

你好,這些是打包好的部署包,將這些直接丟如Tomcat WebApp目錄下就可以通過Web訪問了,如果你想看源碼,用解壓縮軟體都可以的,就看這包裡面有沒有源碼了,zip ,winRAR ,7-zip都可以解壓出來,如果想看源碼,沒有的話,找個反編譯的軟體把class文件拖進去就可以看到了..jd-gui 這個可以,網上找找

㈢ 怎樣用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();
}
}
}

㈣ java解壓zip文件

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();
}
}

}

㈤ 如何在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

㈥ 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目錄

㈦ 怎樣用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) 命令和參數的大小寫是相同意思的,你可以用大寫或者小寫來下命令均可

㈧ 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壓縮包.

閱讀全文

與java中的壓縮包怎麼解壓相關的資料

熱點內容
愛情動作電影 瀏覽:808
八零電子書txt免費下載網站 瀏覽:509
登陸遼事通顯示伺服器連接錯誤怎麼辦 瀏覽:547
9米高隧道演算法 瀏覽:508
池袋最強作品集txt 瀏覽:782
app專題推薦在哪裡 瀏覽:277
神雲伺服器顯示燈 瀏覽:134
程序員磨合期技巧 瀏覽:847
鬼團六全部電影名稱 瀏覽:864
穿越唯一一個女人世界 瀏覽:645
飛言情小說官網入口 瀏覽:581
pdf壓縮後還清晰嗎 瀏覽:654
得到app的電子書書架在哪裡 瀏覽:151
管道彎頭製作演算法 瀏覽:37
phpmvcsmarty實例 瀏覽:925
spring搭建http伺服器地址 瀏覽:713
servlet教程pdf 瀏覽:970
蜂鳥眾包app如何聯系客服 瀏覽:188
程序員t恤淘寶 瀏覽:94
自助研發app如何推廣 瀏覽:727