应用场景:主要使用在对系统文件进行归档,如果中则需要对公文中的附件或正文进行打包归档
示例代码:
解决乱码的问题主要是引用apache的包ant.jar。然后注意下面红色代码部分,java自带的包中也有这两个类。如果乱码最好是采用apache的这两个类。这样才会有设置编码的zos.setEncoding("gbk")这个方法。这样就可解决java zip 打包的知码问题。
下面是演示的java代码:
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipFile; //这里注意不能采用java下面的类包,不然没有设置编码的方法 import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; public class ZipUtil { public static final String ZIP_FILENAME = ""; // 需要解压缩的文件名 public static final String ZIP_DIR = ""; // 需要压缩的文件夹 public static final String UN_ZIP_DIR = ""; // 要解压的文件目录 public static final int BUFFER = 1024; // 缓存大小 public static void main(String[] args) { try { zipFile("F:\page","F:\page\demo.zip"); } catch (Exception e) { e.printStackTrace(); } } public static void zipFile(String baseDir, String fileName) throws Exception { List fileList = getSubFiles(new File(baseDir)); ZipOutputStream zos = new ZipOutputStream( new FileOutputStream(fileName)); ZipEntry ze = null; byte[] buf = new byte[BUFFER]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(baseDir, f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, BUFFER)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.setEncoding("gbk"); zos.close(); } private static String getAbsFileName(String baseDir, File realFileName) { File real = realFileName; File base = new File(baseDir); String ret = real.getName(); while (true) { real = real.getParentFile(); if (real == null) break; if (real.equals(base)) break; else ret = real.getName() + "/" + ret; } return ret; } private static List getSubFiles(File baseDir) { List ret = new ArrayList(); File[] tmp = baseDir.listFiles(); for (int i = 0; i < tmp.length; i++) { if (tmp.isFile()) ret.add(tmp); if (tmp.isDirectory()) ret.addAll(getSubFiles(tmp)); } return ret; } public static void upZipFile() throws Exception { ZipFile zfile = new ZipFile(ZIP_FILENAME); Enumeration zList = zfile.entries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { File f = new File(ZIP_DIR + ze.getName()); f.mkdir(); continue; } OutputStream os = new BufferedOutputStream(new FileOutputStream( getRealFileName(ZIP_DIR, ze.getName()))); InputStream is = new BufferedInputStream(zfile.getInputStream(ze)); int readLen = 0; while ((readLen = is.read(buf, 0, 1024)) != -1) { os.write(buf, 0, readLen); } is.close(); os.close(); } zfile.close(); } public static File getRealFileName(String baseDir, String absFileName) { String[] dirs = absFileName.split("/"); File ret = new File(baseDir); if (dirs.length > 1) { for (int i = 0; i < dirs.length - 1; i++) { ret = new File(ret, dirs); } if (!ret.exists()) ret.mkdirs(); ret = new File(ret, dirs[dirs.length - 1]); return ret; } return ret; } public static void deleteDirFile(String path) { File file = new File(path); if (file.isDirectory()) { // 如果是目录,先递归删除 String[] list = file.list(); for (int i = 0; i < list.length; i++) { deleteDirFile(path + "\" + list); // 先删除目录下的文件 } } file.delete(); } public static String newFolder(String dir) { java.io.File myFilePath = new java.io.File(dir); if (myFilePath.isDirectory()) { } else { myFilePath.mkdirs(); } return dir; } public static String getFileNames(String path) { File file = new File(path); // get file list where the path has File[] array = file.listFiles(); // 获得文件列表 String pdfNames = ""; for (int i = 0; i < array.length; i++) { if (array.isFile()) { if (array.getName().endsWith(".pdf")) { // 获得pdf文件名称 pdfNames += array.getName().substring(0, array.getName().length() - 4) + ","; } } } if (pdfNames.length() > 0) { pdfNames.substring(0, pdfNames.length() - 1); } return pdfNames; } public static void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { // 文件存在时 InputStream inStream = new FileInputStream(oldPath); // 读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; // 字节数文件大小 fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("copy file error!"); e.printStackTrace(); } } public static boolean fileExist(String fileNames, String pdfName) { boolean flag = false; if (!"".equals(fileNames)) { String[] nameArr = fileNames.split(","); for (int i = 0; i < nameArr.length; i++) { if (pdfName.equals(nameArr)) { // 如果文件名相同则执行拷贝操作(拷贝到zip目录准备打包) flag = true; break; } } } return flag; } public static void deleteFileAndDir(String path) { File file = new File(path); File[] array = file.listFiles(); for (int i = 0; i < array.length; i++) { if (array.isFile()) { array.delete(); } else if (array.isDirectory()) { deleteDirFile(array.getPath()); } } } }