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){
}
}
}
Lấy File tài liệu sau khi nén.
Anh ơi làm sao để lấy File zip trả ra đó để gửi vào tài liệu khi gửi Mail ạ. Hiện tại em đang xuất fle ra 1 ổ mặc định còn xuất ra file để thêm vào tài liệu đính kèm email thì mình làm cách nào ạ. Code:
java
get file zip
Remain: 5
2 Answers
monkey
Enlightened
monkey
Enlightened
hướng dẫn này
, em có thể thấy:
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File("path/to/file"));
C:\Users\admin\Desktop\tai-lieu\testing.zip"path/to/file" là được em ạ, em hiểu ý anh chứ?
-
0
anaconda875
Beginner