有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

部署使用java移动文件的方法有哪些?

我正在为我正在制作的一个程序开发一个安装程序,它使用一个zip文件,出于某种原因,我找不到从zip文件夹解包和安装文件的方法。如果有人能帮助我,我会很高兴的


共 (2) 个答案

  1. # 1 楼答案

    虽然java.util.zip和ApacheCommons io提供了很好的机制来从Java中的zip文件中读取数据,但如果您确实想将zip文件解压到保留目录结构的磁盘上,那么最简单的Java API可能就是ApacheAnt

    Project p = new Project();
    p.init();
    Expand ex = new Expand();
    ex.setProject(p);
    ex.setSrc(new File("myArchive.zip"));
    ex.setDest(new File("targetDir"));
    ex.perform();
    
    <> p>但是,正如其他评论家所指出的,您应该考虑使用现有的安装程序构建工具,如IZPack,而不是重新创建车轮。

  2. # 2 楼答案

    尝试以下方法解压缩文件:

    String zipName = "my.zip";
    String installDir = "/home/user/installDir";
    try
    {
        ZipFile zipFile = new ZipFile(zipName);
        files = zipFile.entries();
        while (files.hasMoreElements())
        {
             ZipEntry zipEntry = files.nextElement();
             if(zipEntry.isDirectory())
             {
                    (new File(installDir + zipEntry.getName())).mkdir();
                     continue;
             }
             InputStream in = zipFile.getInputStream(zipEntry);
             File newFile = new File("/home/user/tmp/testdit" + File.separator + zipEntry.getName());
    
             new File(newFile.getParent()).mkdirs();
             FileOutputStream fos = new FileOutputStream(newFile);
             byte[] buffer = new byte[1024];
             int len;
    
             while ((len = in.read(buffer)) >= 0)
             {
                    fos.write(buffer, 0, len);
             }
    
             in.close();
             fos.close();
           }
       } catch (IOException ex)
       {
            ex.printStackTrace();
       }