/** * 遍历要压缩的目录及所有子目录 * @param inputUrl * @param files * @param out * @param data */ public void compressFiles(String inputUrl, File[] files, ZipOutputStream out, byte data[]) { try { for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { String dirPath = inputUrl + files[i].getName() + "/"; File df = new File(dirPath); File dirFiles[] = df.listFiles(); compressFiles(dirPath, dirFiles, out, data); } else { doZip(inputUrl, files[i], out, data); } } } catch (Exception e) { System.out.println(e); } }
/** * 打包压缩文件 * @param inputUrl * @param files * @param out * @param data */ public void doZip(String inputUrl, File files, ZipOutputStream out, byte data[]) { try { FileInputStream fi = new FileInputStream(inputUrl + files.getName()); BufferedInputStream origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(files.getName()); out.putNextEntry(entry); int count; while ( (count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } fi.close(); origin.close(); } catch (IOException e) { System.out.println(e); } }
public int getFiles() { return this.files; }
static public void main(String args[]) { try { ZipFiles_20091120 zf = new ZipFiles_20091120(); // zf.zip("g:/test/", "g:\\test\\temp.zip"); /* String tempFile="g:\\test\\temp.zip"; File file = new File(tempFile); System.out.println((int) file.length());
byte[] buffer = new byte[ (int) file.length()];
BufferedInputStream input = new BufferedInputStream(new FileInputStream(tempFile)); input.read(buffer, 0, buffer.length); input.close(); long b = System.currentTimeMillis(); System.out.println(buffer);