import java.io.File;
import java.io.FileFilter;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.ListIterator;
import java.util.Vector;
import
org.apache.commons.io.filefilter.DirectoryFileFilter;
public
class DeFrankensteiner {
static String untaredRoot =
"/media/big5wf/untared2test";
static Vector<File>
resultDirs = new Vector<File>();
public static void
main(String[] args) {
if (args.length>0) {
if (args[0] != null) {
untaredRoot = args[0];
if(!new
File(untaredRoot).exists()){
System.err.println("Directory does not exist");
}
}
} else {
System.out.println("Program
takes two arguments: root folder of the backup and an optional
target folder");
System.out
.println("Please
rerun and specifiy the root of your untared duplicity backup");
System.out
.println("The
directory contains two folders, 'snapshot' and
'multivol_snapshot'");
System.exit(0);
}
getLeafDirectories(new
File(untaredRoot
+
System.getProperty("file.separator") +
"multivol_snapshot"));
ListIterator iter =
resultDirs.listIterator();
while (iter.hasNext()) {
File sourceDir = (File)
iter.next();
File[] the64KbBlocks =
sourceDir.listFiles();
// We need a non
alphabetic, simple higher is better sort
Arrays.sort(the64KbBlocks,
new IntValueComparator());
String targetFileName =
sourceDir.getAbsolutePath().replace(
"multivol_snapshot",
"snapshot");
System.out.println("Will
save file to " + targetFileName
+ " after merging "
+ the64KbBlocks.length
+ " blocks.");
// instead of /bin/bash try
to use java onboard methods
try {
File targe = new
File(targetFileName);
if(targe.exists())targe.delete();
FileOutputStream fos = new
FileOutputStream(targetFileName,
true);
int i = 0;
for (File file :
the64KbBlocks) {
i++;
FileInputStream fis = new
FileInputStream(file);
byte[] bytesOfA64KbBlock
= bytesOfA64KbBlock = getBytesFromFile(file);
fos.write(bytesOfA64KbBlock);
fis.close();
}
System.err.println("Written file file://" +
targetFileName);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static byte[]
getBytesFromFile(File file) throws IOException {
InputStream is = new
FileInputStream(file);
// Get the size of the file
long length = file.length();
// Create the byte array to
hold the data
byte[] bytes = new
byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset <
bytes.length
&& (numRead =
is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have
been read in
if (offset <
bytes.length) {
throw new
IOException("Could not completely read file "
+ file.getName());
}
// Close the input stream
and return bytes
is.close();
return bytes;
}
public static class
IntValueComparator implements Comparator<File> {
public int compare(final
File file1, final File file2) {
int res = 0;
try {
res =
(Integer.valueOf(file1.getName()) - Integer.valueOf(file2
.getName()));
} catch
(NumberFormatException e) {
System.err.println("Something is here that shouldnt be here:
"
+ e.getMessage());
System.err.println(file1.getAbsolutePath());
System.err.println(file2.getAbsolutePath());
// e.printStackTrace();
System.exit(-1);
}
return res;
}
}
public static void
getLeafDirectories(File dir) {
File listFile[] =
dir.listFiles();
if (listFile != null) {
for (int i = 0; i <
listFile.length; i++) {
if
(listFile[i].isDirectory()) {
File[] subdirs =
listFile[i]
.listFiles((FileFilter)
DirectoryFileFilter.DIRECTORY);
if (subdirs.length == 0)
resultDirs.add(listFile[i]);
getLeafDirectories(listFile[i]);
}
}
}
}
}
|