Rev 148 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* Copyright (C) 2007 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* Author: Steve Ratcliffe
* Create date: 20-Oct-2007
*/
package test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import uk.me.parabola.imgfmt.fs.DirectoryEntry;
import uk.me.parabola.imgfmt.fs.FileSystem;
import uk.me.parabola.imgfmt.fs.ImgChannel;
import uk.me.parabola.imgfmt.sys.ImgFS;
import uk.me.parabola.log.Logger;
/**
* A quick program to copy all of the files out of a .img into separate
* file system files and to create a new .img file based on the files.
* This is a prototype of a program to create a gmapsupp for example.
*
* @author Steve Ratcliffe
*/
public class ExtractFile
{
private static final Logger log =
Logger.
getLogger(ExtractFile.
class);
public static void main
(String[] args
) throws IOException {
String file =
"63240001.img";
if (args.
length > 0)
file = args
[0];
List<String> filterList =
Collections.
emptyList();
if (args.
length > 1)
filterList =
Arrays.
asList(args
).
subList(1, args.
length);
(new ExtractFile
()).
mainprog(file, filterList
);
}
private void mainprog
(String file,
List<String> filterList
) throws IOException {
FileSystem fs = ImgFS.
openFs(file
);
lister
(fs, filterList
);
}
private void lister
(FileSystem fs,
List<String> filterList
) throws IOException {
List<DirectoryEntry
> entries = fs.
list();
System.
out.
println("filter" + filterList
);
for (DirectoryEntry ent : entries
) {
String fullname = ent.
getFullName();
ImgChannel f = fs.
open(fullname,
"r");
try {
String name = displayName
(fullname
);
if (filterList.
isEmpty() || matches
(filterList, name
)) {
System.
out.
format("Copying %-15s %d\n", name, ent.
getSize());
copyToFile
(f, name
);
}
} finally {
f.
close();
}
}
}
private String displayName
(String fullname
) {
return fullname.
trim().
replace("\000",
"");
}
private boolean matches
(List<String> filterList,
String fullname
) {
String name = fullname.
trim().
replace("\000",
"");
for (String s : filterList
) {
if (s.
equalsIgnoreCase(name
))
return true;
}
return false;
}
private void copyToFile
(ImgChannel f,
String name
) {
FileOutputStream os =
null;
ByteBuffer buf =
ByteBuffer.
allocate(8 * 1024);
try {
os =
new FileOutputStream(name
);
FileChannel outchan = os.
getChannel();
while (f.
read(buf
) > 0) {
buf.
flip();
outchan.
write(buf
);
buf.
compact();
}
} catch (FileNotFoundException e
) {
log.
warn("Could not create file");
} catch (IOException e
) {
log.
warn("Failed during copy");
} finally {
if (os
!=
null)
try { os.
close(); }
catch (IOException e
) { /* nothing */ }
}
}
}