简体   繁体   中英

Zipping files downloaded from SFTP server

I am working on a project where I need to download 2 files from an SFTP server, zip them up with a timestamp name and save it to the my local server. Also, once the files are retrieved I want it to send an email that either it successfully completed the job or failed. I am using JSch for retrieveing the SFTP server and could save the files in my local server. Could anyone suggest how should I write my codes to zip the files and send an email. Any help appreciated and I am working with Java for this small project.

Using ZipOutputStream and Java Mail you should be able to do what you want. I created a sample main method that takes in the host, from and to as command line arguments. It assumes you already know the zipped filenames and sends an email with the zip attached. I hope this helps!

public class ZipAndEmail {
  public static void main(String args[]) {
    String host = args[0];
    String from = args[1];
    String to = args[2];

    //Assuming you already have this list of file names
    String[] filenames = new String[]{"filename1", "filename2"};

    try {
      byte[] buf = new byte[1024];
      String zipFilename = "outfile.zip";
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));

      for (int i=0; i<filenames.length; i++) {
        FileInputStream in = new FileInputStream(filenames[i]);
        out.putNextEntry(new ZipEntry(filenames[i]));
        int len;
        while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
      }
      out.close();


      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      Session session = Session.getDefaultInstance(props, null);

      // Define message
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject("Your Zip File is Attached");

      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText("Your zip file is attached");
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      // Part two is attachment
      messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(zipFilename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(zipFilename);
      multipart.addBodyPart(messageBodyPart);

      // Put parts in message
      message.setContent(multipart);

      // Send the message
      Transport.send( message );

      // Finally delete the zip file
      File f = new File(zipFilename);
      f.delete();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM