1、java.util.zip的方式(为了解决文件名的中文问题需要修改源码):
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;
public class Compress_Java {
//文件压缩
public void zip(String zipName, String filePath){
try {
FileOutputStream fos = new FileOutputStream(zipName);
ZipOutputStream out = new ZipOutputStream(fos, "GBK");
File inputFile = new File(filePath);
compress(out, inputFile, null);
System.out.println("压缩完成");
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//压缩逻辑
public void compress(ZipOutputStream zos, File file, String base){
//遍历路径下所有文件进行压缩
byte[] b = new byte[1024];
FileInputStream fis = null;
//处理目录和文件
if(file.isDirectory()){
base = base == null ? file.getName() : base + "/" + file.getName();
try {
zos.putNextEntry(new ZipEntry(base + "/"));
File[] fileList = file.listFiles();
for(int i = 0 ; i < fileList.length ; i++)
{
if(fileList[i].isDirectory())
{
compress(zos, fileList[i], base);
}
else
{
zos.putNextEntry(new ZipEntry(base + "/" + fileList[i].getName()));//这个一定不要忘记了,不然文件是压缩不进入的哦
fis = new FileInputStream(fileList[i]);
while(fis.read(b) != -1)
{
zos.write(b);
}
fis.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
base = base == null ? file.getName() : base + "/" + file.getName();
try {
zos.putNextEntry(new ZipEntry(base));
fis = new FileInputStream(file);
while(fis.read(b) != -1)
{
zos.write(b);
}
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Compress_Java c = new Compress_Java();
c.zip("F:\\ZipTest.zip", "F:\\我的资料");
}
}
修改后的源码会上传到csdn上。
2、apache的方式
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class Compress_Apache {
public void zip(String zipName, String filePath){
try {
FileOutputStream fos = new FileOutputStream(zipName);
ZipOutputStream out = new ZipOutputStream(fos);
File inputFile = new File(filePath);
compress(out, inputFile, null);
System.out.println("压缩完成");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void compress(ZipOutputStream zos, File file, String base){
//遍历路径下所有文件进行压缩
byte[] b = new byte[1024];
FileInputStream fis = null;
//处理目录和文件
if(file.isDirectory()){
base = base == null ? file.getName() : base + "/" + file.getName();
try {
zos.putNextEntry(new ZipEntry(base + "/"));
File[] fileList = file.listFiles();
for(int i = 0 ; i < fileList.length ; i++)
{
if(fileList[i].isDirectory())
{
compress(zos, fileList[i], base);
}
else
{
zos.putNextEntry(new ZipEntry(base + "/" + fileList[i].getName()));
fis = new FileInputStream(fileList[i]);
while(fis.read(b) != -1)
{
zos.write(b);
}
fis.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
base = base == null ? file.getName() : base + "/" + file.getName();
try {
zos.putNextEntry(new ZipEntry(base));
fis = new FileInputStream(file);
while(fis.read(b) != -1)
{
zos.write(b);
}
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Compress_Apache c = new Compress_Apache();
c.zip("F:\\ZipTT.zip", "F:\\我的资料");
}
}