给你一点关键部分的提示
你可能需要用到apache zip这个组件
http://camel.apache.org/zip-dataformat.html
// zipFileName为要解压缩的zip为文件名,例:c:\\filename.zip
// outputDirectoty为解压缩后文件名,例:c:\\filename
public void unZip(String zipFileName, String outputDirectory)
throws Exception {
InputStream in = null;
FileOutputStream out = null;
try {
zipFile = new ZipFile(zipFileName);
java.util.Enumeration e = zipFile.getEntries();
// org.apache.tools.zip.ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
System.out.println("unziping " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("创建目录:" + outputDirectory
+ File.separator + name);
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
// System.out.println("测试文件1:" +fileName);
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0,
fileName.lastIndexOf("/")));
fileName = fileName.substring(
fileName.lastIndexOf("/") + 1, fileName
.length());
}
try {
f = new File(outputDirectory + File.separator
+ zipEntry.getName());
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
byte[] by = new byte[100000];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
} catch (Exception ee) {
} finally {
if (in != null) {
in.close(); //解压完成后注意关闭输入流对象
}
if (out != null) {
out.close(); //解压完成后注意关闭输出流对象
}
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
zipFile.close(); //解压完成后注意关闭apache自带zip流对象
}
}
2. 用java小应用程序实现文件压缩、解压缩
40.ZIP压缩文件
/*
import java.io.*;
import java.util.zip.*;
*/
//创建文件输入流对象
FileInputStream fis=new FileInputStream(%%1);
//创建文件输出流对象
FileOutputStream fos=new FileOutputStream(%%2);
//创建ZIP数据输出流对象
ZipOutputStream zipOut=new ZipOutputStream(fos);
//创建指向压缩原始文件的入口
ZipEntry entry=new ZipEntry(args[0]);
zipOut.putNextEntry(entry);
//向压缩文件中输出数据
int nNumber;
byte[] buffer=new byte[1024];
while((nNumber=fis.read(buffer))!=-1)
zipOut.write(buffer,0,nNumber);
//关闭创建的流对象
zipOut.close();
fos.close();
fis.close();
}
catch(IOException e)
{
System.out.println(e);
}
41.获得应用程序完整路径
String %%1=System.getProperty("user.dir");
42.ZIP解压缩
/*
import java.io.*;
import java.util.zip.*;
*/
try{
//创建文件输入流对象实例
FileInputStream fis=new FileInputStream(%%1);
//创建ZIP压缩格式输入流对象实例
ZipInputStream zipin=new ZipInputStream(fis);
//创建文件输出流对象实例
FileOutputStream fos=new FileOutputStream(%%2);
//获取Entry对象实例
ZipEntry entry=zipin.getNextEntry();
byte[] buffer=new byte[1024];
int nNumber;
while((nNumber=zipin.read(buffer,0,buffer.length))!=-1)
fos.write(buffer,0,nNumber);
//关闭文件流对象
zipin.close();
fos.close();
fis.close();
}
catch(IOException e) {
System.out.println(e);
}
43.递归删除目录中的文件
/*
import java.io.*;
import java.util.*;
*/
ArrayList<String> folderList = new ArrayList<String>();
folderList.add(%%1);
for (int j = 0; j < folderList.size(); j++) {
File file = new File(folderList.get(j));
File[] files = file.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
folderList.add(files[i].getPath());
} else {
fileList.add(files[i]);
}
}
for (File f : fileList) {
f.delete();
}
}
43.ZIP压缩文件夹
/*
http://findjar.com/index.jsp
import java.io.*;
import org.apache.tools.zip.ZipOutputStream; //这个包在ant.jar里,要到官方网下载
//java.util.zip.ZipOutputStream
import java.util.zip.*;
*/
try {
String zipFileName = %%2; //打包后文件名字
File f=new File(%%1);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
String base= "";
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
out.close();
}catch (Exception ex) {
ex.printStackTrace();
}
/*
切,我刚好写了个压缩的,但没写解压的
1. 解压的(参数两个,第一个是你要解压的zip文件全路径,第二个是你解压之后要存放的位置)
/*
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
*/
public class ZipFileList {
public static void main(String[] args) {
extZipFileList(args[0],args[1]);
}
private static void extZipFileList(String zipFileName, String extPlace) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));
ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(extPlace + entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
} else {
FileOutputStream os = new FileOutputStream(extPlace
+ entryName);
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();
}
}
} catch (IOException e) {
}
System.out.println("解压文件成功");
}
}
压缩的(参数最少传两个,第一个是你压缩之后的文件存放的位置以及名字,第二个是你要压缩的文件或者文件夹所在位置,也可以传多个文件或文件夹)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFileOther {
public static String zipFileProcess(ArrayList outputZipFileNameList, String outputZipNameAndPath) {
ArrayList fileNames = new ArrayList();
ArrayList files = new ArrayList();
FileOutputStream fileOut = null;
ZipOutputStream outputStream = null;
FileInputStream fileIn = null;
StringBuffer sb = new StringBuffer(outputZipNameAndPath);
// FileInputStream fileIn =null;
try {
if (outputZipNameAndPath.indexOf(".zip") != -1) {
outputZipNameAndPath = outputZipNameAndPath;
} else {
sb.append(".zip");
outputZipNameAndPath = sb.toString();
}
fileOut = new FileOutputStream(outputZipNameAndPath);
outputStream = new ZipOutputStream(fileOut);
int outputZipFileNameListSize = 0;
if (outputZipFileNameList != null) {
outputZipFileNameListSize = outputZipFileNameList.size();
}
for (int i = 0; i < outputZipFileNameListSize; i++) {
File rootFile = new File(outputZipFileNameList.get(i).toString());
listFile(rootFile, fileNames, files, "");
}
for (int loop = 0; loop < files.size(); loop++) {
fileIn = new FileInputStream((File) files.get(loop));
outputStream.putNextEntry(new ZipEntry((String) fileNames.get(loop)));
byte[] buffer = new byte[1024];
while (fileIn.read(buffer) != -1) {
outputStream.write(buffer);
}
outputStream.closeEntry();
fileIn.close();
}
return outputZipNameAndPath;
} catch (IOException ioe) {
return null;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
if (fileIn != null) {
try {
fileIn.close();
} catch (IOException e) {
}
}
}
}
public static void main(String[] args) {
ArrayList outputZipFileName=new ArrayList();
String savePath="";
int argSize = 0;
if (args != null) {
argSize = args.length;
}
if (argSize > 1) {
if(args[0]!=null)
savePath = args[0];
for(int i=1;i<argSize;i++){
if(args[i]!=null){
outputZipFileName.add(args[i]);
}
}
ZipFileOther instance=new ZipFileOther();
instance.zipFileProcess(outputZipFileName,savePath);
} else {
}
}
private static void listFile(File parentFile, List nameList, List fileList, String directoryName) {
if (parentFile.isDirectory()) {
File[] files = parentFile.listFiles();
for (int loop = 0; loop < files.length; loop++) {
listFile(files[loop], nameList, fileList, directoryName + parentFile.getName() + "/");
}
} else {
fileList.add(parentFile);
nameList.add(directoryName + parentFile.getName());
}
}
}
*/
3. JAVA怎么把zip文件解压到指定位置
刚好我在项目中用到了,送给你,希望你能用上。
/**
* 解压,处理下载的zip工具包文件
*
* @param directory
* 要解压到的目录
* @param zip
* 工具包文件
*
* @throws Exception
* 操作失败时抛出异常
*/
public static void unzipFile(String directory, File zip) throws Exception
{
try
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
ZipEntry ze = zis.getNextEntry();
File parent = new File(directory);
if (!parent.exists() && !parent.mkdirs())
{
throw new Exception("创建解压目录 \"" + parent.getAbsolutePath() + "\" 失败");
}
while (ze != null)
{
String name = ze.getName();
File child = new File(parent, name);
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0)
{
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
ze = zis.getNextEntry();
}
zis.close();
}
catch (IOException e)
{
}
}
4. 为什么JAVA软件压缩包(ZIP RAR)解压后有很多后缀class的文件和文件夹,而没有jar文件
那是java文件的内容,你解压过头了,把jar文件也解压了,那你当然没法看到jar啊,如果你是解压zip文件就解压处class这种文件,建议用文件管理器,改文件的后缀名。如zip的改为jar。这样应该就可以了安装了,不需要再解压了。
希望我的回答对你有用
5. 你好,最近我也遇到用java压缩和解压,向你咨询下你的解决方案什么,你是怎么用文件流方式去压缩
package com.onewaveinc.cwds.commons.utils;
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.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author fz 2010-7-30
* @Description 把指定文件夹下的所有文件压缩为指定文件夹下指定zip文件;把指定文件夹下的zip文件解压到指定目录下
*/
public class ZipUtils {
private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);
private static final String SEPARATE = "/";
/**
* @Author fz 2010-7-30
* @param sourceDir 待压缩目录
* @param zipFile 压缩文件名称
* @throws Exception
* @Description 把sourceDir目录下的所有文件进行zip格式的压缩,保存为指定zip文件
*/
public static void zip(String sourceDir, String zipFile) throws Exception {
OutputStream os = null;
// try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
// 直接压缩单个文件时,取父目录
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
// } catch (Exception e) {
// logger.error("压缩文件或文件夹" + sourceDir + "时发生异常");
// e.printStackTrace();
// }
}
/**
* @Author fz 2010-7-30
* @param source 源文件
* @param basePath 待压缩文件根目录
* @param zos 文件压缩流
* @Description 执行文件压缩成zip文件
*/
private static void zipFile(File source, String basePath, ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
//存相对路径(相对于待压缩的根目录)
String pathName = null;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + SEPARATE;
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
logger.error("压缩文件" + source + "时发生异常");
e.printStackTrace();
}
}
/**
* @Author fz 2010-7-30
* @param zipfile 待解压文件
* @param destDir 解压文件存储目录
* @throws Exception
* @Description 解压zip文件,只能解压zip文件
*/
@SuppressWarnings("unchecked")
public static void unZip(String zipfile, String destDir) throws Exception {
destDir = destDir.endsWith(SEPARATE) ? destDir : destDir + SEPARATE;
byte b[] = new byte[1024];
int length;
ZipFile zipFile;
// try {
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists()) {
loadFile.getParentFile().mkdirs();
}
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
outputStream.close();
inputStream.close();
}
}
zipFile.close();
// } catch (IOException e) {
// logger.error("解压文件" + zipfile + "时发生异常");
// e.printStackTrace();
// }
}
}
6. java中将一个文件夹下所有的文件压缩成一个文件,然后,解压到指定目录.
import java.io.*;
import java.util.zip.*;
public class CompressD {
// 缓冲
static byte[] buffer = new byte[2048];
public static void main(String[] args) throws Exception {
// 来源
File inputDir = new File("C:\\CompressTest\\");
// 目标
FileOutputStream fos = new FileOutputStream("C:\\CompressTest.zip");
// 过滤
ZipOutputStream zos = new ZipOutputStream(fos);
// 压缩
zip(inputDir.listFiles(), "", zos);
// 关闭
zos.close();
}
private static void zip(File[] files, String baseFolder, ZipOutputStream zos)
throws Exception {
// 输入
FileInputStream fis = null;
// 条目
ZipEntry entry = null;
// 数目
int count = 0;
for (File file : files) {
if (file.isDirectory()) {
// 递归
zip(file.listFiles(), file.getName() + File.separator, zos);
continue;
}
entry = new ZipEntry(baseFolder + file.getName());
// 加入
zos.putNextEntry(entry);
fis = new FileInputStream(file);
// 读取
while ((count = fis.read(buffer, 0, buffer.length)) != -1)
// 写入
zos.write(buffer, 0, count);
}
}
}
7. 请大神帮忙解决一个用java解压缩一个zip压缩格式字节流中文内容乱码问题!
这个问题我有点印象,好像是包的问题。好像不能用zip的那个,换另一个包就好了。具体我也不记得了
8. java 解压zip中文文件保存
用jdk自带的zip工具做压缩时,对中文支持不是很好。
建议使用ant.jar中的压缩和解压缩工具