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(); } }
详细解决方案
j2se-zip
热度:5104 发布时间:2013-02-25 00:00:00.0
相关解决方案
- 请问:J2SE URLDecode出错
- j2se 上拉框变化字体
- j2se-nio-FileLock
- j2se-metadata
- J2SE 5.0的HotSpot JVM下的GC学习 - ParallelGC
- J2SE 5.0的HotSpot JVM下的GC学习 - ParallelCompactingGC
- j2SE 总揽
- J2EE、J2SE、J2ME的容易区别
- J2SE 杂感
- j2se-可变参数列表
- J2SE 5.0的HotSpot JVM下的GC学习 - 分代、GC类型、快速分配
- j2se-socket的缓冲区议论
- j2se-java中,怎么获得用户当前的工作目录
- j2se-Java异步socket
- J2SE J2EE J2ME的差异 (转)
- J2SE JPanel 空布局嵌套有关问题
- j2se-zip
- Build path specifies execution environment J2SE-1.4 异常
- j2se-同步的Map
- J2se 基础温习
- j2se-String.spilt,或("|")分隔符有关问题
- log4j跟spring的配置文件位置-J2SE
- j2me 兑现 j2se 的 Properties 功能
- J2SE JPanel的缩放有关问题
- j2se-clone
- J2SE 试题解决办法
- “文件夹路径 My Documents中包含无效字符”的异常-J2SE Runtime Environment安装时出现
- 哪位知道J2EE、J2ME、J2SE 各是什么东西? 是怎样的关系?解决思路
- 何位知道J2EE、J2ME、J2SE 各是什么东西? 是怎样的关系
- j2se-File种和RandomAccessFile类[转]