Java UnZip错误?

时间:2014-01-04 05:29:50

标签: java unzip

我正在制作一个客户端更新程序&在尝试解压缩文件时,我的cmd中出现错误。

它应该做的是:

  • 检查文件是否存在于用户的家中(user.home),
  • 如果用户家中不存在,请检查项目目录中是否存在该文件,
  • 如果项目目录中不存在,请下载该文件;如果是,则将文件解压缩到用户家中。

错误:

 Exists in Directory!
file unzip : C:\Users\Ryan T\Desktop\Rezzion Updater\.rezzion.cache\rezzion.cache
java.io.FileNotFoundException: .rezzion.cache\rezzion.cache\Data (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at rezzion.UnZip.unZipIt(UnZip.java:54)
    at rezzion.Downloader.<init>(Downloader.java:68)
    at rezzion.Downloader.main(Downloader.java:78)
file unzip : C:\Users\Ryan T\Desktop\Rezzion Updater\.rezzion.cache\rezzion.cache\Data

项目文件夹的图片: enter image description here

我的Downloader.java(主类):

    package rezzion;

import java.awt.FlowLayout;

public class Downloader extends JFrame {
    private static final long serialVersionUID = 1L;

    private static boolean exists = (new File(System.getProperty("user.home") + ".rezzion.cache")).exists();
    private static boolean existsinDir = (new File("rezzion.cache.zip")).exists();
    private static String site = "https://dl.dropboxusercontent.com/s/yoh4d17gfgnv2od/rezzion.cache.zip?dl=1&token_hash=AAE1qdxL_-2y_arb8MBnk8AHSsuhLH1-lwSiGVc0ayQKXA";
    private static String filename = "rezzion.cache.zip";
    private static final String INPUT_ZIP_FILE = "rezzion.cache.zip";
    private static final String OUTPUT_FOLDER = ".rezzion.cache";
    private final int BUFFER = 1024;

    public Downloader() {
        JFrame frm = new JFrame();
        JProgressBar current = new JProgressBar(0, 100);
        current.setBounds(35, 68, 326, 30);
        current.setValue(0);
        current.setStringPainted(true);
        frm.setVisible(true);
        frm.getContentPane().setLayout(null);
        frm.setSize(400, 200);
        frm.setDefaultCloseOperation(EXIT_ON_CLOSE);

        if (!exists) {
            if (!existsinDir) {
            frm.getContentPane().add(current);
        try {
            URL url = new URL(site);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int filesize = connection.getContentLength();
            float totalDataRead = 0;
            java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
            java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
            java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER);
            byte[] data = new byte[BUFFER];
            int i = 0;
            while ((i = in.read(data, 0, BUFFER)) >= 0) {
                totalDataRead = totalDataRead + i;
                bout.write(data, 0, i);
                float Percent = (totalDataRead * 100) / filesize;
                current.setValue((int) Percent);
                if (current.getValue() == 99) {
                    current.setValue(100);
                }
            }
            bout.close();
            in.close();
        } catch (Exception e) {
            javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, e.getMessage(), "Error", javax.swing.JOptionPane.DEFAULT_OPTION);
        }
        } else {
            System.out.println("Exists in Directory!");
            UnZip unZip = new UnZip();
            unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
            //TODO: Exists in Directory
        }
        } else {
            System.out.println("Exists in User.Home!");
            //TODO: Exists in User.Home
        }
    }

    public static final void main(String[] args) throws Exception {
        new Downloader();
    }

    /**
     * 
     */
    public static void unzipComplete() {
        //TODO
        System.out.println("unzip Complete!");
    }
}

我的UnZip.java(自我解释):

package rezzion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnZip {
    List<String> fileList;
    private static final String OUTPUT_FOLDER = ".rezzion.cache";

    public void unZipIt(String zipFile, String outputFolder) {

        byte[] buffer = new byte[1024];

        try {

            // create output directory is not exists
            File folder = new File(System.getProperty("user.home")
                    + OUTPUT_FOLDER);
            if (!folder.exists()) {
                folder.mkdir();
            }

            // get the zip file content
            ZipInputStream zis = new ZipInputStream(
                    new FileInputStream(zipFile));
            // get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {

                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator
                        + fileName);

                System.out.println("file unzip : " + newFile.getAbsoluteFile());

                // create all non exists folders
                // else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();

            System.out.println("Done");
            Downloader.unzipComplete();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

很抱歉这篇文章格式不佳,但是如果有人可以提供帮助我已经阅读了这个错误了一点&amp;仔细看看它告诉我的是什么,尝试合并课程&amp;没运气。感谢那些愿意指出一些事情的人,即使它显而易见。我很想念它。

1 个答案:

答案 0 :(得分:1)

由于这显然是一个桌面应用程序,答案对我来说似乎很明显。对于部署Java桌面应用程序,最好的选择通常是安装应用程序。使用Java Web Start。 JWS适用于Windows,OS X&amp; * nix中。

  

JWS提供了许多吸引人的功能,包括但不限于启动画面,桌面集成,文件关联,自动更新(包括延迟下载和程序化控制更新),本地分区和&amp;按平台,体系结构或语言环境下载的其他资源,运行时环境的配置(最低J2SE版本,运行时选项,RAM等),使用扩展轻松管理公共资源..


  

..有没有办法让我保持这个?

没有。

  • 使用自动更新,您将放弃所有这些,只需让JWS客户端处理它。
  • “程序化控制更新”使用的API仅适用于应用。使用JWS启动,特别是DownloadServiceListener