0

I have this updater class that updates my program by unzipping a zip file. It works good except when I add in a folder for it to extract.. I have included the 3 methods that I use to accomplish this but the problem must lie in the unzip() method. Once it prints Extracting: New folder/TEST2.txt it makes the error posted below. I have tried making the folder without spaces and it throws the same error. The contests of the zip file are just 2 text files and a folder.

Structure of zip:

New Folder >Test2.txt

Test1.txt

Errors:

java.io.IOException: The system cannot find the path specified at java.io.WinNTFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(Unknown Source) at update.Main_Gui.unzip(Main_Gui.java:244) at update.Main_Gui.access$3(Main_Gui.java:230) at update.Main_Gui$3.run(Main_Gui.java:108) at java.lang.Thread.run(Unknown Source)

Code:

private void copyFiles(File f, String dir) throws IOException {
    File[] files = f.listFiles();
    for (File ff : files) {
        if (ff.isDirectory()) {
            new File(dir + "/" + ff.getName()).mkdir();
            copyFiles(ff, dir + "/" + ff.getName());
        } else {
            copy(ff.getAbsolutePath(), dir + "/" + ff.getName());
        }

    }
}

public void copy(String srFile, String dtFile)
        throws FileNotFoundException, IOException {

    File f1 = new File(srFile);
    File f2 = new File(dtFile);

    InputStream in = new FileInputStream(f1);

    OutputStream out = new FileOutputStream(f2);

    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

private void unzip() throws IOException {
    int BUFFER = 2048;
    BufferedOutputStream dest = null;
    BufferedInputStream is = null;
    ZipEntry entry;
    ZipFile zipfile = new ZipFile("update.zip");
    Enumeration e = zipfile.entries();
    (new File(root)).mkdir();
    while (e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();
        outText.setText(outText.getText() + "\nExtracting: " + entry);
        if (entry.isDirectory())
            (new File(root + entry.getName())).mkdir();
        else {
            (new File(root + entry.getName())).createNewFile();
            is = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[BUFFER];
            FileOutputStream fos = new FileOutputStream(root
                    + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            //zipfile.close();
            dest.flush();
            dest.close();
            is.close();
        }
    }

}
javajoejuan
  • 323
  • 3
  • 9
  • What's the contents of your zip file? From the errors and your output, your OS cannot find the path "New Folder/". Does http://stackoverflow.com/questions/13846000/file-separators-of-path-name-of-zipentry solve your problem? – Wai Leong Aug 06 '16 at 03:03
  • just two text files, one in `New Folder` and one in the `root`. – javajoejuan Aug 06 '16 at 03:27
  • Then, don't you need to create every folder leading up to the file TEST2.txt? – Wai Leong Aug 06 '16 at 03:44

0 Answers0