StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
public void zipFiles(){ FileOutputStream fos = null; ZipOutputStream zipOut = null; FileInputStream fis = null; try { File folder = new File("D:\Data\attachment"); File[] attachFiles = folder.listFiles(); fos = new FileOutputStream("C:\Users\admin\Desktop\tai-lieu\testing.zip"); zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); for(File file: attachFiles){ fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(file.getName()); System.out.println("Zipping the file: "+file.getName()); zipOut.putNextEntry(ze); byte[] tmp = new byte[1024]; int size = 0; while((size = fis.read(tmp)) != -1){ zipOut.write(tmp, 0, size); } zipOut.flush(); fis.close(); } zipOut.close(); System.out.println("Done... Zipped the files..."); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try{ if(fos != null) fos.close(); } catch(Exception ex){ } } }
MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(new File("path/to/file"));
C:\Users\admin\Desktop\tai-lieu\testing.zip
"path/to/file"
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Input your email to receive reset password link, or if you remember your password, you can click