public class CompressUtil
{
public static String getName(String path)// 为了获得不在根目录的文件路径 如C: emp
ewTemp 其不带根目录的路径是 temp
ewTemp
{
int i = path.indexOf('\');
path = path.substring(i + 1);
// System.out.println(path);
return path;
}
// 将文件压缩到zip输出流中
public static void fileToZip(String fileName, ZipOutputStream out)
{
FileInputStream in = null;
ZipEntry entry = null;
byte[] buffer = new byte[4096];
int bytes = 0;
File file = new File(fileName);
if (file.isFile())
{
try
{
in = new FileInputStream(file);
entry = new ZipEntry(getName(file.getAbsolutePath()));// 压缩后的文件内容要放在此文件内
out.putNextEntry(entry);// 必须要有这一步 否则会编译出错
while ((bytes = in.read(buffer)) != -1)
{
out.write(buffer, 0, bytes);
}
out.closeEntry();
in.close();
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}
// 压缩目录
public static void dirToZip(String yuanFielName, ZipOutputStream out)
{
File fileYuan = new File(yuanFielName);
if (fileYuan.isDirectory())
{
File[] files = fileYuan.listFiles();
if (files.length == 0)
{
String s = getName(fileYuan.getAbsolutePath()) + "/";// 空文件夹要存的是目录 否则就当文件存储了 如果是 最后的也是文件的形式 谁知道为什么
ZipEntry entry = new ZipEntry(s);
try
{
out.putNextEntry(entry);
out.closeEntry();
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
dirToZip(files[i].getAbsolutePath(), out);// 递归压缩文件目录
else
fileToZip(files[i].getAbsolutePath(), out);
}
}
}
public static void zipFile(String yuanWenJianName, String targetFileName)
{
File fileYuan = new File(yuanWenJianName);
if (!fileYuan.exists())
{
System.out.println("根目录不存在");
return;
}
File targetFile = new File(targetFileName);// 目标文件
try
{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
targetFile.getAbsolutePath()));// 创建一个zip输出流来压缩数据并写入zip文件中
if (fileYuan.isDirectory())// 将目录下的所有文件都压缩到zip
{
System.out.println(fileYuan.getAbsolutePath() + "是目录");
CompressUtil.dirToZip(fileYuan.getAbsolutePath(), out);
}
if (fileYuan.isFile())
{
System.out.println(fileYuan.getAbsolutePath() + "是文件");
CompressUtil.fileToZip(fileYuan.getAbsolutePath(), out);
}
out.close();
System.out.println("压缩文件成功");
} catch (Exception e)
{
System.out.println("压缩文件失败" + e);
e.printStackTrace();
// TODO: handle exception
}
}
// 解压文件
public static void upZipFile(String yuanWenJianName, String muBiaoWenjianName)
{
if (!muBiaoWenjianName.endsWith(File.separator))
muBiaoWenjianName += File.separator;
try
{
ZipFile zipFile = new ZipFile(yuanWenJianName);
ZipEntry entry = null;
Enumeration entrys = zipFile.entries();
while (entrys.hasMoreElements())
{
entry = (ZipEntry) entrys.nextElement();
String entryName = entry.getName();
String mubiaoName = muBiaoWenjianName + entryName;
System.out.println(muBiaoWenjianName);
if (entry.isDirectory())
new File(mubiaoName).mkdirs();
else
{
new File(mubiaoName).getParentFile().mkdirs();
System.out.println();
}
File file = new File(mubiaoName);
FileOutputStream out = new FileOutputStream(file);
InputStream in = zipFile.getInputStream(entry);
int bytes = 0;
byte[] buffer = new byte[1024];
while ((bytes = in.read(buffer)) != -1)
{
out.write(buffer, 0, bytes);
}
out.close();
in.close();
System.out.println("解压文件成功");
}
} catch (Exception e)
{
// TODO: handle exception
System.out.println("解压文件失败");
e.printStackTrace();
}
}
public static void main(String[] args)
{
String yuanWenJianName1 = "C:/temp/";
String yuanWenJianName2 = "C:/temp/newTemp.pdf";
String targetFileName = "C:/haha.zip";// 压缩之后的为文件名字
zipFile(yuanWenJianName1, targetFileName);
upZipFile(targetFileName, "C:/haha");
// zipFile(yuanWenJianName2, targetFileName);
}
}