Java 多个资料打包
Java 多个文件打包
public class ArchiveCreator { private String _zipFilePath = ""; private String _imageFolderPath = ""; private String _zipFileName = ""; /** * @return the _zipFileName */ public String getZipFileName() { return _zipFileName; } /** * @param zipFileNameIn the _zipFileName to set */ public void setZipFileName(String zipFileNameIn) { _zipFileName = zipFileNameIn; } private static final int BUFFER = 2048; private String _error = ""; public ArchiveCreator(){} public void setZipFilePath(String path){_zipFilePath = path;} public void setImageFolderPath(String path){_imageFolderPath = path;} public String getError(){return _error;} public void zipEm() { File directory = new File(_imageFolderPath); File files[] = directory.listFiles(); try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(_zipFilePath+_zipFileName); ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream( dest)); byte data[] = new byte[BUFFER]; File f = null; ZipEntry entry = null; FileInputStream fi = null; int count; // Iterate over the file list and add them to the zip file. for (int i = 0; i < files.length; i++) { fi = new FileInputStream(files[i]); origin = new BufferedInputStream(fi, BUFFER); entry = new ZipEntry(files[i].getName()); zip.putNextEntry(entry); while ((count = origin.read(data, 0, BUFFER)) != -1) { zip.write(data, 0, count); } count = 0; origin.close(); } zip.close(); } catch (Exception e) { e.printStackTrace(); _error = e.getMessage(); } } public static void main(String[] args){ System.out.println("In here"); ArchiveCreator archnst = new ArchiveCreator(); archnst.setImageFolderPath("D:\\cbmo\\"); archnst.setZipFilePath("d://cbmo//"); archnst.setZipFileName("testcode.zip"); archnst.zipEm(); System.out.println("At End"); } }