當前位置:首頁 » 文件管理 » java解壓文件夾

java解壓文件夾

發布時間: 2022-11-13 17:57:15

『壹』 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程序問題

確認123目錄是否在壓縮文件下,即目錄是否已壓縮,若沒有壓縮,需要人為指定123目錄,即outfile = new File("d:"+File.separator+entry.getName());
需改為outfile = new File("d:\\123"+File.separator+entry.getName());
你應該是這種情況,下面情況供參考
若文件夾已壓縮在壓縮文件,你創建文件夾時需要使用outfile.mkdir();或outfile.mkdirs();
否則會將123創建成d盤一個無擴展名的文件

『肆』 如何通過java ZipInStream類將壓縮文件解壓到指定的文件夾中

通過ZipInStream類將壓縮文件解壓到指定的文件夾中:
源程序是:
import java.io.*;
import java.util.zip.*;
public class Decompressing { // 創建文件
public static void main(String[] temp) {
ZipInputStream zin; // 創建ZipInputStream對象
try { // try語句捕獲可能發生的異常
zin = new ZipInputStream(new FileInputStream("F:/hello.zip"));
// 實例化對象,指明要進行解壓的文件
ZipEntry entry = zin.getNextEntry(); // 獲取下一個ZipEntry
while (((entry = zin.getNextEntry()) != null)
&& !entry.isDirectory()) {
// 如果entry不為空,並不在同一目錄下
File file = new File("F:\" + entry.getName()); // 獲取文件目錄
System.out.println(file);
if (!file.exists()) { // 如果該文件不存在
file.mkdirs();// 創建文件所在文件夾
file.createNewFile(); // 創建文件
}
zin.closeEntry(); // 關閉當前entry
System.out.println(entry.getName() + "解壓成功");
}
zin.close(); // 關閉流
} catch (Exception e) {
e.printStackTrace();
}
}
}

『伍』 java ant解壓怎麼獲取解壓後的文件夾名

(1)還是先上代碼結構:


『陸』 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中怎麼用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中不能解壓壓縮包下面的文件夾

Java原生解壓縮API是無法操作中文的,你的問題在於沒有判斷是否是目錄或者文件,如果你把目錄當成文件寫到磁碟,肯定不對

packageorg.xdemo.superutil;

importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedInputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipInputStream;
importjava.util.zip.ZipOutputStream;

/**
*文件解壓縮
*@author<ahref="

*[email protected]
*/
publicclassZipUtils{
/**
*文件解壓縮注意不能有中文文件名
*@paramzipSrczip文件路徑
*@paramdest解壓的路徑,無需以「\」或者「/」結尾
*@throwsIOException
*/
publicstaticvoinzip(StringzipSrc,Stringdest)throwsIOException{

//為了適配不同操作系統的文件路徑的分隔符
StringsystemSeparator=System.getProperty("file.separator");
System.out.println(systemSeparator);

CheckedInputStreamcis=newCheckedInputStream(newFileInputStream(newFile(zipSrc)),newCRC32());
ZipInputStreamzis=newZipInputStream(cis);
ZipEntryze=null;
FileOutputStreamfos=null;
byte[]buffer=newbyte[1024];
intlength=0;

if(!newFile(dest).exists())newFile(dest).mkdirs();

while((ze=zis.getNextEntry())!=null){
StringfileName=dest+File.separator+ze.getName();
//都替換一遍省心
fileName=fileName.replace("\",systemSeparator).replace("/",systemSeparator);
if(fileName.lastIndexOf(systemSeparator)!=-1)newFile(fileName.substring(0,fileName.lastIndexOf(systemSeparator))).mkdirs();
if(fileName.lastIndexOf(systemSeparator)==fileName.length()-1)continue;
System.out.println(fileName);
fos=newFileOutputStream(fileName);
while((length=zis.read(buffer))!=-1){
fos.write(buffer,0,length);
}
fos.close();
}
zis.closeEntry();
zis.close();
}


}

『玖』 java新人,在學習解壓縮文件時,解壓文件後,裡面的文件變文件夾

file.mkdirs();只有目錄才要創建文件夾,如果這個file本應該是文件,你卻創建了一個同名文件夾,那自然不能再創建這個文件了(即後面的file.createNewFile();無法創建文件).

另外,對於文件其實是不需要createNewFile的,因為如果你真要解壓文件,你要打開對應文件流,並且創一個輸出流,輸出到目標文件中(這個文件會自動創建的),但你的代碼裡面沒看到這個步驟,給你個完整的解壓示例.

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.Enumeration;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;

publicclassTest
{
publicstaticvoinZipFiles(StringzipPath,StringdescDir)throwsIOException
{
unZipFiles(newFile(zipPath),descDir);
}

publicstaticvoinZipFiles(FilezipFile,StringdescDir)throwsIOException
{
FilepathFile=newFile(descDir);
if(!pathFile.exists())
{
pathFile.mkdirs();
}
ZipFilezip=newZipFile(zipFile);
for(Enumerationentries=zip.entries();entries.hasMoreElements();)
{
ZipEntryentry=(ZipEntry)entries.nextElement();
StringzipEntryName=entry.getName();
InputStreamin=zip.getInputStream(entry);
StringoutPath=(descDir+zipEntryName).replaceAll("\*","/");

//獲取當前file的父路徑,這才是文件夾
Filefile=newFile(outPath.substring(0,outPath.lastIndexOf('/')));

//判斷路徑是否存在,不存在則創建文件路徑
if(!file.exists())
{
file.mkdirs();
}
//判斷文件全路徑是否為文件夾,如果是上面已經上傳,不需要解壓
if(newFile(outPath).isDirectory())
{
continue;
}
//輸出文件路徑信息
System.out.println(outPath);

OutputStreamout=newFileOutputStream(outPath);
byte[]buf1=newbyte[1024];
intlen;
while((len=in.read(buf1))>0)
{
out.write(buf1,0,len);
}
in.close();
out.close();
}
System.out.println("******************解壓完畢********************");
}

publicstaticvoidmain(String[]args)throwsException
{
unZipFiles(newFile("d:/test.zip"),"e:/");
}
}

『拾』 java里怎麼解壓tar.gz文件啊,網上好多例子都不行

我覺得你的步驟有問題,tar.gz壓縮包里放文件或文件夾都無所謂,需要用程序來生成,下面詳細說明:
1.
用程序中的方法【archive】生成tar壓縮文件
2.
用程序中的方法【compressArchive】生成tar.gz壓縮文件
3.
將生成的壓縮文件為參數進行解壓,具體是:
unCompressArchiveGz("d:\\test\\xmlbak.tar.gz");//解壓
4.
查看解壓後的文件夾內容和文件內容,均可以正常顯示訪問
樓主的問題主要是手動生成了一個壓縮文件,這是主要的問題原因。

熱點內容
傳奇腳本刷怪 發布:2024-10-06 11:57:47 瀏覽:261
c語言輸入小寫輸出大寫 發布:2024-10-06 11:49:57 瀏覽:361
金立手機伺服器異常是什麼原因 發布:2024-10-06 11:49:48 瀏覽:699
python多線程假的 發布:2024-10-06 11:37:09 瀏覽:723
自己動手構造編譯 發布:2024-10-06 11:35:11 瀏覽:550
c語言編譯器win10 發布:2024-10-06 11:33:35 瀏覽:971
安卓手機里的自動備份是什麼 發布:2024-10-06 11:30:16 瀏覽:714
想買電腦配置要注意哪些 發布:2024-10-06 11:21:50 瀏覽:541
滴滴雲存儲 發布:2024-10-06 11:17:37 瀏覽:767
精通android游戲開發 發布:2024-10-06 11:16:54 瀏覽:801