1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| public static void main(String[] args) {
try {
Class.forName("org.hsqldb.jdbcDriver");
}
catch(Exception x) {
System.out.println("Unable to load the driver class!");
}
Connection con = null; //Database objects
Statement com = null;
ResultSet rec = null;
ZipFile file = null; //For handling zip files
ZipEntry ent = null;
Enumeration en = null; //For the entries in the zip file
BufferedOutputStream out = null; //For the output from the zip class
InputStream in = null; //for reading buffers from the zip file
File f = null; //Used to get a temporary file name, not actually used for anything
int len; //General length counter for loops
List<String> v = new ArrayList<String>(); //Stores list of unzipped file for deletion at end of program
//Unzip zip file, via info from
//http://www.devx.com/getHelpOn/10MinuteSolution/20447
try {
//Open the zip file that holds the OO.Org Base file
file = new ZipFile("/Users/eclipse/Projet/BDtest.odb");
//Create a generic temp file. I only need to get the filename from
//the tempfile to prefix the extracted files for OO Base
f = File.createTempFile("ooTempDatabase", "tmp");
f.deleteOnExit();
//Get file entries from the zipfile and loop through all of them
en = file.entries();
while (en.hasMoreElements()) {
//Get the current element
ent = (ZipEntry)en.nextElement();
//If the file is in the database directory, extract it to our
//temp folder using the temp filename above as a prefix
if (ent.getName().startsWith("database/")) {
System.out.println("Extracting File: " + ent.getName());
byte[] buffer = new byte[1024];
//Create an input stream file the file entry
in = file.getInputStream(ent);
//Create a output stream to write out the entry to, using the
//temp filename created above
out = new BufferedOutputStream(new FileOutputStream("/tmp/" + f.getName() + "." + ent.getName().substring(9)));
//Add the newly created temp file to the tempfile vector for deleting
//later on
v.add("/tmp/" + f.getName() + "." + ent.getName().substring(9));
//Read the input file into the buffer, then write out to
//the output file
while((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
//close both the input stream and the output stream
out.close();
in.close();
}
}
//Close the zip file since the temp files have been created
file.close();
//Create our JDBC connection based on the temp filename used above
try {
con = DriverManager.getConnection("jdbc:hsqldb:file:/tmp/" + f.getName(), "SA", "");
}
catch (SQLException x) {
System.out.println("Couldn't get connection!");
} |
Partager