package p02_advanced.L18_IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
// import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
// import java.util.zip.ZipInputStream; // 只能操作ZIP文件
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry; // 有点像系统为文件维护的结构体
import java.util.zip.ZipFile; // 只能操作ZIP文件
import java.util.Enumeration;
/**
* @author Fravel DWon
*
* @brief need JDK 1.8 or upper, a demo for zip compress and decompress
*
*/
public class L18_07_ZipFile {
public static void main(String[] args) throws Exception {
String rootpath = System.getProperty("user.dir");
String relative_path = "\\src\\p02_advanced\\L18_IO\\";
String fname = "test_source_folder";
String srcpath = rootpath + relative_path;
String dstpath = rootpath + relative_path;
System.out.println(srcpath + "\n" + dstpath); // 这一堆就是设置个文件路径;)
/* 与zip0交替注释测试这段代码 */
Zip zip = new Zip(dstpath, srcpath, fname);
zip.compress();
// Zip zip0 = new Zip(srcpath, dstpath + "/" + fname + ".zip");
// zip0.decompress();
}
}
class Zip {
private final static int BUFSIZ = 8192;
private File srcfp; // 压缩时负责遍历源文件夹, 起始位置为源目录/文件, 解压时指zip文件
private File dstfp; // 代表解压时的目标文件夹
private String entname; // 负责寄存起始的entry名, 内容为源目录或文件的名称
private ZipOutputStream zout;
/* zout负责生成zip文件, 由目标目录参数dstpath和文件入口名称entname负责初始化
*
* 因为流在压缩过程不能被关闭, 而如果通过声明压缩文件引用dstfp作为参数传入封装的静态方法在
* 方法内开启流的话, 等于是开启了一个新的流, 这样会冲掉前一次静态函数调用时输出的结果, 所以
* 舍弃了dstfp作为参数在方法内开启流, 转而使用zout作为参数直接传入静态方法以供调用
*/
/**
* @brief 压缩时的构造方法
*
* @param dstpath 目标路径
* @param srcpath 源路径
* @param entname 文件名/文件夹名
* @throws FileNotFoundException
*/
public Zip(String dstpath, String srcpath, String entname)
throws FileNotFoundException {
String zfname = dstpath + entname + ".zip";
String srcfname = srcpath + entname;
FileOutputStream zout = new FileOutputStream(new File(zfname));
this.zout = new ZipOutputStream(zout);
this.srcfp = new File(srcfname);
this.entname = entname;
}
/**
* @brief 解压时的构造方法
*
* @param dst_decompress_path 目标解压路径
* @param srczfp_fullpath 源文件的绝对路径
* @throws FileNotFoundException
*/
public Zip(String dst_decompress_path, String srczfp_fullpath)
throws FileNotFoundException {
this.dstfp = new File(dst_decompress_path);
this.srcfp = new File(srczfp_fullpath);
this.zout = null;
}
/**
* @brief 压缩的方法
*
* @return
* @throws Exception
*/
public boolean compress() throws Exception {
if (!srcfp.exists() || zout == null)
return false;
if (srcfp.isFile())
fcompress(zout, srcfp, entname);
else
dircompress(zout, srcfp, entname);
zout.close();
return true;
}
/**
* @brief 文件夹压缩
*
* @param zout 写/创建 zip文件的输出流
* @param srcdir 源路径文件夹
* @param entname entry的名称
* @throws Exception
*/
private static void
dircompress(ZipOutputStream zout, File srcdir, String entname)
throws Exception {
zout.putNextEntry(new ZipEntry(entname + "/")); // 这句主要, 建立压缩信息头
zout.closeEntry();
File[] fps = srcdir.listFiles();
if (fps.length != 0)
for (int i = 0; i < fps.length; ++i) {
String fname = entname + "/" + fps[i].getName(); // 新的entry名称
if (fps[i].isFile())
fcompress(zout, fps[i], fname);
else
dircompress(zout, fps[i], fname);
}
}
/**
* @brief 文件压缩方法
*
* @param zout 写/创建 zip文件的输出流
* @param srcfp 源路径文件
* @param entname entry的名称
* @throws Exception
*/
private static void
fcompress(ZipOutputStream zout, File srcfp, String entname) throws Exception {
if (!srcfp.exists())
{
System.out.println("The target file:\n" + srcfp.getAbsolutePath() +
srcfp.getName() + "\ndoes not exists");
return;
}
try (
InputStream in = new BufferedInputStream(new FileInputStream(srcfp));
) /* 缓存到内存读 */
{
zout.putNextEntry(new ZipEntry(entname)); // 这句主要, 建立压缩信息头
// BufferedOutputStream zout_ = new BufferedOutputStream(zout);
/* 缓存到buffer写, 用BufferedOutputStream太重,
* 直接分配个缓冲区用 */
byte[] buf = new byte[BUFSIZ];
int readlen = 0; // 每次写入的字节长度
for (; (readlen = in.read(buf)) != -1;) {
zout.write(buf, 0, readlen);
}
zout.closeEntry();
if (in != null)
in.close();
}
}
/**
* @brief 解压的方法
*
* @return
* @throws Exception
*/
public boolean decompress() throws Exception {
if (zout != null || !srcfp.exists())
return false;
try (
ZipFile zfp = new ZipFile(srcfp)
)
{
Enumeration<? extends ZipEntry> ents = zfp.entries();
ZipEntry ent = null;
for (; ents.hasMoreElements();) {
ent = (ZipEntry)ents.nextElement();
System.out.println("decompress--> " + ent.getName());
if (ent.isDirectory())
dirdecompress(dstfp, ent);
else
fdecompress(dstfp, zfp, ent);
}
}
return true;
}
/**
* @brief 文件夹解压
*
* @param dstdir 目标文件夹
* @param ent entry结构
*/
private static void dirdecompress(File dstdir, ZipEntry ent) {
String dir_path = dstdir.getAbsolutePath() + "/" + ent.getName();
File dir = new File(dir_path);
dir.mkdirs();
}
/**
* @brief 文件解压
*
* @param dstfp 目标文件
* @param zfp 源压缩文件
* @param ent entry结构
* @throws IOException
*/
private static void
fdecompress(File dstfp, ZipFile zfp, ZipEntry ent)
throws IOException {
File fp = new File(dstfp.getAbsolutePath() + "/" + ent.getName());
if (!fp.exists()) {
String parent_dir_path = fp.getParent();
File parent_dir = new File(parent_dir_path);
parent_dir.mkdirs();
}
fp.createNewFile();
// 将压缩文件内容写入到文件中
try (
InputStream in = new BufferedInputStream(zfp.getInputStream(ent));
FileOutputStream fout = new FileOutputStream(fp);
)
{
int readlen;
byte[] buf = new byte[BUFSIZ];
for (; (readlen = in.read(buf)) != -1;) {
fout.write(buf, 0, readlen);
}
in.close();
fout.close();
}
}
}
解压算法来源: