In java: take a snapshot of the directory and restore it.

Is there a library to take a folder, take a snapshot of its contents, make some changes, and then restore it to its previous state directly from the java program (i.e. not from the command line)?

Edit: Basically, I am working on a very large folder: 80 MB, ~ 7000 files. And I want to restore only files that were changed as quickly as possible. Just copying everything back takes a lot of time.

+4
source share
6 answers

A snapshot is basically a recursive copy of all directories, and this seems inevitable. From a recovery point of view, simply delete the directory and rename the temporary directory with the original name.

If this is for functional testing, how about having a well-known reference and copying it at the beginning of the test? Thus, snapshots are not taken. This only works, of course, if you always start with a well-known set of files.

Regarding the actual recursive copy, Apache has a method for doing this in Commons-IO, as well as for a recursive deletion .

+2
source

I do not know about a non-standard library for what you want, but you can try copying the contents of the directory to a temporary directory and copy it back when you are done (and delete the temporary directory).

Take a look at java.io.File

And here is another library specifically designed for copying files / folders.

To your board: you need to find a way to track or β€œflag” files that have changed. Maybe a file class extension with this flag property? You can store all the files in memory, but then you will have to worry about running out of memory if these directories become too large.

+2
source

Yes! The API functionality is enough for this!

Do you want to take a recursive snapshot?

I like groovy syntax:

currentDir.eachFileRecurse{ file -> ...} 
0
source

Not exactly Java, but gives less headache ...

 public void before(String origDir, String tmpDir) { try { Runtime.getRuntime().exec("cp -pr " + origDir + " " + tmpDir).waitFor(); } catch (IOException err) { ... } } public void after(String origDir, String tmpDir) { try { String rndDir = createRandomName(); Runtime.getRuntime().exec("mv " + origDir + " " + rndDir).waitFor(); Runtime.getRuntime().exec("mv " + tmpDir + " " + origDir).waitFor(); Runtime.getRuntime().exec("rm " + rndDir); } catch (IOException err) { ... } } 
0
source

I would look at the apo-commons IO library . In particular, I used DirectoryWalker, which is very convenient to recursively check the contents of a directory.

0
source

Watching the directory (and subdirectories) for changes, accumulating them, and then when you want to undo the changes, copy only the changed files.

See for example:

http://twit88.com/blog/2007/10/02/develop-a-java-file-watcher/

http://www2.hawaii.edu/~qzhang/FileSystemWatcher/index.html

0
source

All Articles