// 需要引入org.apache.ant 包 public static void main(String[] args) throws IOException { String text = "{n" + " "regions": [n" + " {n" + " "name": "string",n" + " "referenceNo": "string",n" + " "warehouses": [n" + " {n" + " "address": "string",n" + " "coordiante": {n" + " "coordsys": "string",n" + " "latitude": 0,n" + " "longitude": 0n" + " },n" + " "name": "string",n" + " "referenceNo": "string"n" + " }n" + " ]n" + " }n" + " ]n" + "}"; File file = File.createTempFile("file", ".txt"); FileOutputStream fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } // get the content in bytes byte[] contentInBytes = text.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); final File test = listToZip(Lists.newArrayList(file), "test"); System.out.println(test.getPath()); } public static File listToZip(Listlist, String zipfilename) { FileInputStream is = null; File f = null; ZipOutputStream zos = null; try { if (list != null && list.size() > 0) { // String uri = GlobalConfig.getConfigValue("zipFile.path"); f = File.createTempFile(zipfilename, ".zip"); if (!f.exists()) { f.mkdirs(); } // 创建zip文件输出流 zos = new ZipOutputStream(new FileOutputStream(f)); zos.setEncoding("GBK"); for (int i = 0; i < list.size(); i++) { File file = list.get(i); if (file.exists()) { // 创建源文件输入流 is = new FileInputStream(file); zos.putNextEntry(new ZipEntry(file.getName())); byte[] buf = new byte[2048]; int length = -1; while ((length = is.read(buf)) != -1) { zos.write(buf, 0, length); zos.flush(); } zos.closeEntry(); is.close(); } else { System.out.println("源文件不存在"); } } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (zos != null) { zos.close(); } } catch (IOException e) { e.printStackTrace(); } } return f; }



