Archiving old files

Situation:

We currently compress files as we operate with them, and write them to our archive folder on our ftp server. We now plan to write the uncompressed files to a short-term archive folder. We would like a weekly process that steps through the short-term archive, finds the files that have been there for a set period of time (like a week or a month), compresses them, and moves them to a long-term archive folder. We'd like to do this with a plugin, but can't find any pre-defined java classes to work with. Does Adeptia provide any Java classes we can use to connect to our ftp server and manipulate files as described?

Solution:

Please refer to the steps below to write a Custom Plugin code:

1.Use "org.apache.commons.net.ftp.FTPClient" class to connect and login on FTP server and changedDirectory Path to the source directory.

2.Make zip output Stream using "java.util.zip.ZipOutputStream" class.

3.List all FTPFile in an array calling "listFiles()" method of FTPClient class.

4.Process each file using for loop start index value from 2 to length of fileList array.

5.Check whether file was modified one week before from current date.

6.If it meets the criterion.Create inputstream of input file using "retrieveFileStream()" method of FTPClient class.

7.Make entry of this file in zip output stream using putNextEntry() method of "ZipOutputStream" class by passing an object "java.util.zip.ZipEntry" class.

For e.g;
// out : Object of zip output stream.
// fileName : File Name which meets the criteria.

out.putNextEntry(new ZipEntry(fileName));

8.Write input stream to zip output stream.

9.Flush zip output stream by calling flush() method on "ZipOutputStream" class object.

10.Close zip entry by calling closeEntry() method on "ZipOutputStream" class object.

11.In the end disconnect from FTP by calling disconnect() method on FTPClient class.

12.Close all input/output streams.

Note : Handle ZipException and IOException.

 

Please also find the attached jar file that contains various class files related to FTP.

Have more questions? Submit a request

0 Comments

Article is closed for comments.