1、用java.util.zip下的工具压缩
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class UtilZip { public static void main(String[] args) throws Exception{ String srcFileName="D:/mail.eml"; String zipFileName="D:/mail.zip"; try { zipFile(srcFileName,zipFileName); } catch (Exception e) { e.printStackTrace(); } } public static void zipFile(String srcFileName,String zipFileName) throws Exception{ FileInputStream fis=null; ZipOutputStream zos=null; File srcFile=new File(srcFileName); fis=new FileInputStream(srcFile); zos=new ZipOutputStream(new FileOutputStream(new File(zipFileName))); zos.putNextEntry(new ZipEntry(srcFile.getName())); byte[] buffer=new byte[4028]; int readed=0; while((readed=fis.read(buffer))!=-1){ zos.write(buffer,0, readed); } //这里一定要关闭流,否则压缩文件会损坏 zos.closeEntry(); zos.close(); fis.close(); } }
2、用ant.jar包里面的工具压缩
import java.io.*; import org.apache.tools.zip.ZipOutputStream; public class AntZip { public static void main(String arg[]) { String srcFilePath = new String("D:/邮件"); String outFilename = new String("D:/mail.rar"); AntZip.zipFileByAnt(srcFilePath, outFilename); } public static void zipFileByAnt(String srcFilePath,String zipFileName) { try { File srcPath = new File(srcFilePath); int fileCount = srcPath.listFiles().length; String[] filenames = new String[fileCount]; byte[] buffer = new byte[1024]; File[] files = srcPath.listFiles(); for (int i = 0; i < fileCount; i++) { filenames[i] = srcPath.getPath() + File.separator + files[i].getName(); } ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); out.setEncoding("UTF-8"); FileInputStream fis=null; for (int i = 0; i < filenames.length; i++) { fis = new FileInputStream(filenames[i]); out.putNextEntry(new org.apache.tools.zip.ZipEntry(files[i].getName())); int readed; while ((readed = fis.read(buffer)) > 0) { out.write(buffer, 0, readed); } } out.closeEntry(); out.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } }
3、压缩文件
package jp.com.syspro.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; public class ZipFile { public static void main(String[] args) { zip("D:/Temp/DWR与界面开发", "D:/Temp/DWR与界面开发.zip"); } public static void zip(String srcFile){ zip(srcFile,""); } public static void zip(String srcFile, String zipFile) { File src = new File(srcFile); if (src.exists()) { // 压缩文件不存在时,压缩到当前目录 if ("".equals(zipFile) || zipFile == null) { if (src.isDirectory()) { zipFile = srcFile + ".zip"; } else { zipFile = srcFile.split("\\.")[0] + ".zip"; } } else { if (!zipFile.contains(srcFile)) { File zip = new File(zipFile); if (!zip.getParentFile().exists()) { zip.getParentFile().mkdirs(); } } else { // 压缩文件在被压缩的文件夹里面时,压缩到当前目录 zipFile = srcFile + ".zip"; } } zip(src, zipFile); } } public static void zip(File file, String zipFile) { try { FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); zip(zos, file, ""); zos.close(); } catch (IOException e) { e.printStackTrace(); } } public static void zip(ZipOutputStream zos, File file, String base) { zos.setEncoding("UTF-8"); if (file.isDirectory()) { File[] files = file.listFiles(); try { zos.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < files.length; i++) { zip(zos, files[i], base + files[i].getName()); } } catch (IOException e) { e.printStackTrace(); } } else { try { zos.putNextEntry(new ZipEntry(base)); FileInputStream fis = new FileInputStream(file); int readed = 0; while ((readed = fis.read()) != -1) { zos.write(readed); } zos.flush(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }