栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用JarOutputStream创建JAR文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用JarOutputStream创建JAR文件?

事实证明,这JarOutputStream有三个未记录的怪癖:

  1. 目录名称必须以“ /”斜杠结尾。
  2. 路径必须使用“ /”斜杠,而不是“ ”
  3. 条目不能以“ /”斜杠开头。

这是创建Jar文件的正确方法:

public void run() throws IOException{  Manifest manifest = new Manifest();  manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");  JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);  add(new File("inputDirectory"), target);  target.close();}private void add(File source, JarOutputStream target) throws IOException{  BufferedInputStream in = null;  try  {    if (source.isDirectory())    {      String name = source.getPath().replace("\", "/");      if (!name.isEmpty())      {        if (!name.endsWith("/"))          name += "/";        JarEntry entry = new JarEntry(name);        entry.setTime(source.lastModified());        target.putNextEntry(entry);        target.closeEntry();      }      for (File nestedFile: source.listFiles())        add(nestedFile, target);      return;    }    JarEntry entry = new JarEntry(source.getPath().replace("\", "/"));    entry.setTime(source.lastModified());    target.putNextEntry(entry);    in = new BufferedInputStream(new FileInputStream(source));    byte[] buffer = new byte[1024];    while (true)    {      int count = in.read(buffer);      if (count == -1)        break;      target.write(buffer, 0, count);    }    target.closeEntry();  }  finally  {    if (in != null)      in.close();  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号