当前位置:首页 » 文件管理 » 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.
查看解压后的文件夹内容和文件内容,均可以正常显示访问
楼主的问题主要是手动生成了一个压缩文件,这是主要的问题原因。

热点内容
fpga编程语言 发布:2024-10-06 10:29:24 浏览:341
python按时间排序 发布:2024-10-06 10:02:50 浏览:214
安卓收款机下载什么应用能收款 发布:2024-10-06 09:38:29 浏览:1000
java初级工程师面试题 发布:2024-10-06 09:37:49 浏览:217
知鸟在哪里修改密码 发布:2024-10-06 09:37:10 浏览:303
怎么更改微信钱包密码 发布:2024-10-06 09:28:08 浏览:549
控制中心不支持配置怎么办 发布:2024-10-06 09:16:39 浏览:811
地暖存储罐 发布:2024-10-06 09:10:19 浏览:580
搭建模型服务器 发布:2024-10-06 09:05:23 浏览:845
java使用类 发布:2024-10-06 09:05:22 浏览:931