-2

I have developed a program where I want to save different files to a zip as backup and then load them back when a restore button is clicked. I have the save file code below but how would I load this file with JFileChooser? I don't need to read it to the program just literally unzip it into the folder where my application is. How would I do this? My create zip code is below:

public void createZip(){

    byte[] buffer = new byte[1024];
    String[] srcFiles = {"Payments.dat", "PaymentsPosted.dat", "Receipts.dat", "ReceiptsPosted.dat", "AccountDetails.dat", "AssetsLiabilities.dat", "UnitDetails.dat"};
    String zipFile = "Backups.zip";

    try{

        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);

        for (int i=0; i < srcFiles.length; i++) {
            File srcFile = new File(srcFiles[i]);
            FileInputStream fis = new FileInputStream(srcFile);

            // begin writing a new ZIP entry, positions the stream to the start of the entry data
            zos.putNextEntry(new ZipEntry(srcFile.getName()));

            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }

            zos.closeEntry();
            // close the InputStream
            fis.close();
        }

        // close the ZipOutputStream
        zos.close();

        }catch(IOException ex){
           ex.printStackTrace();
        }
        JOptionPane.showMessageDialog(null,"File Saved! See Backups.zip in your program folder");
    }
}

Also I would appreciate if someone could tell me how to also wrap the above method into a JFileChooser to save?

Line
  • 1,529
  • 3
  • 18
  • 42
eoinDub
  • 81
  • 2
  • 4
  • 10
  • Q: Why not just turn "Backups.zip" into a method parameter, and then call createZip (with your new parameter) from your JFileChooser handler? – FoggyDay Jun 05 '14 at 19:47
  • Hmm never thought of that thanks! – eoinDub Jun 05 '14 at 21:54
  • 1
    1. you should ask one question at a time, not mix them 2. you should accept answer which helped you (and probably upvote it) 3. don't post a solution in original question - better do in in separate answer if you think it's worth it (you can even accept you solution then if it's the best in your opinion) – Line Aug 09 '18 at 10:07

1 Answers1

2

To create the JFileChooser, the code would look like this:

public void showOpenDialog() {

    // Create a filter so that we only see .zip files
    FileFilter filter = new FileNameExtensionFilter(null, "zip");

    // Create and show the file filter
    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(filter);
    int response = fc.showOpenDialog(null);

    // Check the user pressed OK, and not Cancel.
    if (response == JFileChooser.APPROVE_OPTION) {
        File yourZip = fc.getSelectedFile();
        // Do whatever you want with the file
        // ...
    }        
}

As far as actually unzipping the zipped files, you can find more info here.

user184994
  • 17,791
  • 1
  • 46
  • 52
  • Thanks! With your code and the one from your link I was able to tinker with it and get it working. Now I have a jFileChooser that will select only zips and open each zip directly into the program folder where they should be :) – eoinDub Jun 05 '14 at 22:07
  • You just have to get rid of the . before the zip – eoinDub Jun 05 '14 at 22:10