jfilechooser - Create a .zip file in Java with images -
i'm creating java program gets images jfilechooser , create .zip file containing selected images. files code:
final jfilechooser fc = new jfilechooser(); fc.setmultiselectionenabled(true); fc.setfilefilter(new filenameextensionfilter("image files", "bmp", "png", "jpg")); fc.setacceptallfilefilterused(false); fc.showopendialog(null); file files[] = fc.getselectedfiles();
how create .zip file containing files of files[]
array?
thank :d.
file somefile = new file("somefile.zip"); file files[] = fc.getselectedfiles(); bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream(somefile)); // create zip file first try (zipoutputstream out = new zipoutputstream(bos)) { // write files/copy archive (file file : files) { // put new zip entry output stream every file out.putnextentry(new zipentry(file.getname())); files.copy(file.topath(), out); out.closeentry(); } }
Comments
Post a Comment