Question

Well I'm generating 2 .xml files, but I want to save them in a folder, and also zip them, I don't want to generate them separately, but separated in a folder and zipped. d Did you get that?

I want to put both of the files that I'm creating into a folder and zip it. The only thing that I want to save is the zip file.

String name = fields.find(chooser.getLocation(), "mimic");
                Mimic mimic = getMimic(mimicList.get(0));
                String fileName = chooser.getSelectedFile().toString() + File.separator + "des_" +nombreMimic.substring(0, nombreMimic.length()-4)+ ".xml";
                String config = chooser.getSelectedFile().toString() + File.separator + "cfg_" +nombreMimic.substring(0, nombreMimic.length()-4)+".xml";
                FileOutputStream file;
                FileOutputStream file2;


                file = new FileOutputStream(fileName);
                file2 =new FileOutputStream(config);

                Parser parser;
                parser = new Parser(file,new String[]{});
                parser.render(mimic , fields);
                 JOptionPane.showMessageDialog(null, "Complete!");

                Parser2 parser2;
                parser2 = new Parser2(file2,new String[]{});
                parser2.render(mimic , fields);
                JOptionPane.showMessageDialog(null, "Complete!");   


                FileInputStream inputStream = new FileInputStream(chooser.getSelectedFile().toString() + File.separator + "des_" +nombreMimic.substring(0, nombreMimic.length()-4)+ ".xml");
                ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(nombreMimic.substring(0, nombreMimic.length()-4)+".des"));

                zip.putNextEntry(new ZipEntry(fileName));

                byte [] buffer = new byte [1024];
                int bytesRead;
                while((bytesRead=inputStream.read(buffer))>0){

                zip.write(buffer,0,bytesRead);

                }

                zip.closeEntry();
                zip.close();
                inputStream.close();


            } catch (Exception ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                JOptionPane.showMessageDialog(null, "Error parsing");
            }
        }
    }
}                                          
Was it helpful?

Solution

It looks like the answer to your problem was answered here: how to zip a folder itself using java

There it is explained how to zip a file/folder using Java.

You have to save the files in the folder for this to work, but once it is zipped, you can delete the original folder following this tutorial which uses:

String path = "/path/to/file";
Files.delete(path);

This way the only thing saved will be the zip file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top