日期:2014-05-20 浏览次数:20602 次
private InitData getInitFile(String path) { Properties p = new Properties(); try { ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream( new FileInputStream(path))); FileOutputStream fileOut = null; ZipEntry entry = null; byte[] buf = new byte[1024]; while ((entry = zipIn.getNextEntry()) != null) { if (entry.getName().endsWith(InitData.INIT_FILE_NAME)) { p.load(zipIn); } else { zipIn.closeEntry(); } } zipIn.close(); } catch (Exception e) { logger.debug("Putting instrumented entry: error=" + e.getMessage(), e); } return new InitData(p); } 下面是把往zip里面塞一个序列化文件 public static void main(String[] args) { try { String path = "d:/aaa.war"; ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream( new FileInputStream(path))); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream( "d:/aaa1.war")); ZipEntry entry = null; while ((entry = zipIn.getNextEntry()) != null) { ZipEntry zipOutEntry = new ZipEntry(entry.getName()); zipOutEntry.setComment(entry.getComment()); zipOutEntry.setExtra(entry.getExtra()); zipOutEntry.setTime(entry.getTime()); zipOut.putNextEntry(zipOutEntry); byte[] entryBytes = IOUtil .createByteArrayFromInputStream(zipIn); zipOut.write(entryBytes); zipOut.flush(); zipOut.closeEntry(); } Test t = new Test(); t.setName("aaaa"); ZipEntry outputEntry = new ZipEntry("test.ser"); zipOut.putNextEntry(outputEntry); ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(b); o.writeObject(t); byte[] entryBytes = b.toByteArray(); zipOut.write(entryBytes); zipOut.closeEntry(); zipIn.close(); zipOut.close(); File f = new File(path); f.delete(); File ff = new File("d:/aaa1.war"); ff.renameTo(f); } catch (Exception e) { System.out.println(e); } } //下面这个是解压的 public void setFileItem(FileItem fileItem) throws Exception { String baseDir = super.getSrcPath(); BufferedOutputStream dest = null; ZipInputStream zis = new ZipInputStream(new BufferedInputStream( fileItem.getInputStream())); ZipEntry entry; this.mkdirs(baseDir); while ((entry = zis.getNextEntry()) != null) { int count; byte data[] = new byte[BUFFER]; // write the files to the disk String entryName = entry.getName(); String path = baseDir + entryName; File file = new File(path); if (entry.isDirectory()) { file.mkdirs(); } else { this.mkdirs(path); FileOutputStream fos = new FileOutputStream(path); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } } if(dest!=null){ dest.flush(); dest.close(); } zis.closeEntry(); } zis.close(); this.srcPath = baseDir + fileItem.getFieldName(); } private void mkdirs(String path) { path = path.replace("\\", "/"); path = path.substring(0, path.lastIndexOf("/")); File file = new File(path); if (!file.exists()) file.mkdirs(); } 删除目录 private void deleteDirs(File file) { if (file.isDirectory()) { File[] files = file.listFiles(); for (File f : files) { if (f.isFile()) f.delete(); else { deleteDirs(f); } } file.delete(); } else { file.delete(); } }