-2

In my application I want to zip a file or folder from a given userinput. When I try to get the input from a JDialog, thats working fine but if I want to try to let the user choose from a fileChooser, my programm wont work - its always creating an empty zip file. Please can you teach me how to fix that?

EDIT: When I try to get filename and outputname by JDialog, thats working fine but when I want to pick the filename by a filechooser I can't pass it to my further functions the correct way. Maybe it could be because the directory separator? It writes the filename and also the filepath but when I pass it it wont work.

import java.io.*;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.JFileChooser;

public class Zipper { 

int prefixLength;
  ZipOutputStream zipOut;
  byte[] ioBuffer = new byte[4096];

  public Zipper(String dirFileName, String dirFileOutput) throws Exception
  { prefixLength = dirFileName.lastIndexOf("/") + 1;
    zipOut = new ZipOutputStream(new FileOutputStream("./" + dirFileOutput + ".zip"));
    createZipFrom(new File(dirFileName));
    zipOut.close();
  }

  void createZipFrom(File dir) throws Exception
  { if (dir.exists() && dir.canRead() && dir.isDirectory())
    { File[] files = dir.listFiles();
      if (files != null)
      { for (File file: files)
        { if (file.isDirectory()) 
          { createZipFrom(file);
          }
          else
          { String filePath = file.getPath();//.replace('\\', '/');
            FileInputStream in = new FileInputStream(filePath);
            zipOut.putNextEntry(new ZipEntry(filePath.substring(prefixLength)));
            int bytesRead;
            while ((bytesRead = in.read(ioBuffer)) > 0) 
            { zipOut.write(ioBuffer, 0, bytesRead);
            }
            System.out.println(filePath + " added\n");
            zipOut.closeEntry();
            in.close();
          }
        }
      }
    }
  }

  public static void main(String[] args) throws Exception {

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.showOpenDialog(null);
    File selectedFile = fileChooser.getSelectedFile();
    System.out.println(selectedFile.getPath());

    String dirFileName = selectedFile.getPath(); // should come from the fileChooser but isnt working

    String dirFileOutput = JOptionPane.showInputDialog(null, "packetname"); // thats working..

    System.out.println(dirFileName);
    System.out.println(dirFileOutput);

    new Zipper(dirFileName, dirFileOutput);

    System.out.println("package " + dirFileOutput + "." + ".zip created\n");

  }

}

EDIT: I got it running with changing

prefixLength = dirFileName.lastIndexOf("/") + 1;

to this

prefixLength = dirFileName.lastIndexOf("\\") + 1;
peterh
  • 11,875
  • 18
  • 85
  • 108
BrainWorx
  • 103
  • 1
  • 13
  • What is it supposed to do, and what does it actually do instead? – user253751 Feb 06 '15 at 11:00
  • 1
    So what you're saying is "It's supposed to work, and instead it doesn't work" – user253751 Feb 06 '15 at 11:01
  • right, and now? if i know more i wouldnt ask.. but i try to edit my question. lets see – BrainWorx Feb 06 '15 at 11:03
  • You don't know what happens when you run the program? – user253751 Feb 06 '15 at 11:04
  • please see my question again i wrote it now more specific and you can even look my full code. maybe you can see the problem now. – BrainWorx Feb 06 '15 at 11:07
  • Change `public Zipper(String dirFileName, String dirFileOutput)` to `public Zipper(File file)` and all should work.. – Andrew Thompson Feb 06 '15 at 11:15
  • BTW - where did you find that ..unusual way (with code on the same line as `{`) of structuring the code lines? – Andrew Thompson Feb 06 '15 at 11:17
  • i got he example from here but without the filechooser http://codehelper.24.eu/158501/verzeichnis-in-eine-zip-datei-packen – BrainWorx Feb 06 '15 at 11:32
  • Do you mean instead of `String dirFileName = selectedFile.getPath();` i should use `File dirFileName = selectedFile` and pass it to the Zipper Class as `File dirFileName` – BrainWorx Feb 10 '15 at 01:45
  • You got the downvotes because you can't write a sentence on your first language without a spelling mistake. I think you had to visit more often the elementary school. – peterh Mar 16 '15 at 01:47

1 Answers1

2

You don't check the return value. Please read the JFileChooserDialog javadoc:

JFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser, see How to Use File Choosers, a section in The Java Tutorial. The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}

The following code works for me:

        // ...
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(appFrame);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println(selectedFile.getPath());

            String dirFileName = selectedFile.getPath(); // should come from the fileChooser but isnt working

            String dirFileOutput = JOptionPane.showInputDialog(null, "packetname"); // thats working..

            System.out.println(dirFileName);
            System.out.println(dirFileOutput);

            System.out.println("package " + dirFileOutput + "." + ".zip created\n");
        }
cghislai
  • 1,751
  • 15
  • 29
  • Thank you! This minute i tried the if (result == JFileChooser.APPROVE_OPTION) { function and have seen it works. Thank you a lot! – BrainWorx Feb 06 '15 at 11:16
  • it doesnt worked for me in that way but the right answer was to check the return value. thanks – BrainWorx Feb 06 '15 at 11:29
  • Hmm but anyway the filename seems to couldnt be passed correctly becuse the created zip folder is empty. – BrainWorx Feb 06 '15 at 11:56