package mms;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipFile;
import java.util.Enumeration;
public class zipAndUnzip {
static final int BUFFER = 8192;
//压缩
int zip(File srcFiles[],String outZipFile){
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(outZipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
for (int i = 0; i < srcFiles.length; i++) {
FileInputStream fi = new FileInputStream(srcFiles[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(srcFiles[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
fi.close();
origin.close();
}
out.close();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 1;
}
}
//解压
//srcZipFiles 压缩文件路径
//outFilePath 解压文件输出路径
int unzip(String srcZipFiles,String outFilePath){
System.out.println("srcZipFiles:"+srcZipFiles);
System.out.println("outFilePath:"+outFilePath);
ZipFile zipFile = new ZipFile(srcZipFiles);
Enumeration<? extends ZipEntry> emu = zipFile.entries();
String unzip_dir = srcZipFiles.substring(srcZipFiles.lastIndexOf("/")+1,srcZipFiles.lastIndexOf(".") );
String new_dir = "";
File file = null;
boolean flag = false;
while(emu.hasMoreElements()){
ZipEntry entry = (ZipEntry)emu.nextElement();
System.out.println("entry.getName():"+entry.getName());
if(entry.getName().indexOf("/")>=0){
new_dir = entry.getName().substring(0,entry.getName().lastIndexOf("/"));
}else{
new_dir = unzip_dir;
flag = true;
}
System.out.println("new_dir:"+new_dir);
//会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
if (entry.isDirectory())
{
if(!flag){
new File(outFilePath + entry.getName().replace(new_dir,unzip_dir)).mkdirs();
}else{
new File(outFilePath + new_dir+"/"+entry.getName()).mkdirs();
}
//new File(outFilePath + entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
if(!flag){
file = new File(outFilePath + entry.getName().replace(new_dir,unzip_dir));
}else{
file = new File(outFilePath + new_dir+"/"+entry.getName());
}
//File file = new File(outFilePath + entry.getName());
//加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
//而这个文件所在的目录还没有出现过,所以要建出目录来。
File parent = file.getParentFile();
if(parent != null && (!parent.exists())){
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1)
{
bos.write(data, 0, count);
}
bos.flush();
bos.close();
bis.close();
}
zipFile.close();
return 0;
}
}
相关推荐
zip格式压缩和解压简单操作 可有你想学的
实现了文件的ZIP格式压缩和zip压缩文件的解压,带有界面。
VB6.0调用 DLL版ZIP压缩文件夹目录和解压ZIP文件,网上很多源码不好用,所以就自己做了一个。原始代码是VC++的用VS2019编译了一个,DLL封装了下。调用很简单 '调用方法如下: 'CreateZipFileA "C:\123", "C:\123.zip...
通过设计,允许创建ZIP压缩文件,并对ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作。GUI界面与下图类似: 【实验目的】 要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件...
zip文件压缩和解压工具类: 1.compressDirectory该方法提供将一个目录压缩为一个压缩包 2.unzip该方法提供解压缩文件
QT5 zip压缩和解压,支持文件夹的压缩和解压源代码,接口简洁,使用简单
该工具支持zip的压缩和解压 1 1
自己封装的文件ZIP压缩和解压接口,调用简单方便
C++利用Zlib库实现zip文件压缩及解压 支持递归压缩.可配合自动更新功能实现zip压缩包进得软件更新
c#zip压缩与解压c#zip压缩与解压c#zip压缩与解压c#zip压缩与解压c#zip压缩与解压
zip 文件压缩 解压源码,c++版。已测试,100%可用! // XUnzip.cpp Version 1.3
C语言实现Zip压缩解压
NULL 博文链接:https://zhenyong.iteye.com/blog/1576148
ZIP格式压缩文件工具类
Zip压缩和解压文件,支持多个文件一起压缩,有测试代码,欢迎参考
内容概要:qt5 调用 7zip库的压缩解压功能,且通过信号发出压缩解压的进度,主程序在bin目录中。 适用人群:qt5开发人员 适用场景:用的编译环境是qt5.12.2+vs2017,系统是windows10. lib目录中OleAut32.Lib和User32....
vbs解压和压缩zip的,都有了,好用才放上来给大家下载,代码来自网上,整理一下,方便大家! 不是调用winrar的那种, 主要用到 Shell.Application对象
java压缩解压ZIP
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
Android端zip压缩与解压,目前暂时只做zip格式支持,基于Zip4j (http://www.lingala.net/zip4j/)进行扩展成工具类,支持对单个文件,多个文件以及文件夹进行压缩,对压缩文件解压到到指定目录,支持压缩解压使用密码...