压缩代码
package com.swan.zib.compression;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 将文件夹下面的文件
* 打包成zip压缩文件
*
* @author admin
*/
public final class FileZib {
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
*
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static boolean zip(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("待解压文件不存在");
} else {
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
if (zipFile.exists()) {
System.out.println("此文件已存在");
} else {
File[] sourceFiles = sourceFile.listFiles();
if (sourceFile == null || sourceFiles.length < 1) {
System.out.println("待压缩文件目录不存在任何文件");
} else {
try {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bytes = new byte[1024 * 10];
for (int i = 0; i < sourceFiles.length; i++) {
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(bytes, 0, 1024 * 10)) != -1) {
System.out.println(fileName + "正在压缩");
zos.write(bytes, 0, read);
}
}
flag = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zos != null) {
try {
zos.close();
zos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
return flag;
}
public static void main(String[] args) {
String sourceFilePath = "/Users/dllo/Desktop/DJ0703-简历1";
String zipFilePath = "/Users/dllo/Desktop/临时文件";
String fileName = "小柯压缩4";
boolean flag = FileZib.zip(sourceFilePath, zipFilePath, fileName);
if (flag) {
System.out.println("文件压缩成功");
} else {
System.out.println("文件压缩失败");
}
}
}
解压代码
package com.swan.zib.compression;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Created by dllo on 18/1/4.
*/
public class ReFileZib {
/**
* 解压文件
*
* @param
* @param
* @return
*/
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
ZipInputStream Zin = new ZipInputStream(new FileInputStream(
"/Users/dllo/Desktop/临时文件/小柯压缩3.zip"));
BufferedInputStream Bin = new BufferedInputStream(Zin);
String Parent = "/Users/dllo/Desktop/临时文件";
File Fout = null;
ZipEntry entry;
try {
while ((entry = Zin.getNextEntry()) != null && !entry.isDirectory()) {
Fout = new File(Parent, entry.getName());
if (!Fout.exists()) {
(new File(Fout.getParent())).mkdirs();
}
FileOutputStream out = new FileOutputStream(Fout);
BufferedOutputStream Bout = new BufferedOutputStream(out);
int b;
while ((b = Bin.read()) != -1) {
Bout.write(b);
}
Bout.close();
out.close();
System.out.println(Fout + "解压成功");
}
Bin.close();
Zin.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("耗费时间: " + (endTime - startTime) + " ms");
}
}