| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.samples; |
| 7 |
|
| 8 |
import java.io.BufferedInputStream; |
| 9 |
import java.io.BufferedOutputStream; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.InputStream; |
| 12 |
import java.io.InputStreamReader; |
| 13 |
import java.io.LineNumberReader; |
| 14 |
import java.io.OutputStream; |
| 15 |
import java.io.OutputStreamWriter; |
| 16 |
import java.io.PrintWriter; |
| 17 |
import java.sql.Connection; |
| 18 |
import java.sql.DriverManager; |
| 19 |
import org.h2.engine.Constants; |
| 20 |
import org.h2.security.SHA256; |
| 21 |
import org.h2.store.FileStore; |
| 22 |
import org.h2.store.FileStoreInputStream; |
| 23 |
import org.h2.store.FileStoreOutputStream; |
| 24 |
import org.h2.store.fs.FileUtils; |
| 25 |
import org.h2.tools.CompressTool; |
| 26 |
import org.h2.tools.RunScript; |
| 27 |
import org.h2.tools.Script; |
| 28 |
|
| 29 |
/** |
| 30 |
* This sample application shows how to manually |
| 31 |
* create an encrypted and compressed script file. |
| 32 |
*/ |
| 33 |
public class CreateScriptFile { |
| 34 |
|
| 35 |
/** |
| 36 |
* This method is called when executing this sample application from the |
| 37 |
* command line. |
| 38 |
* |
| 39 |
* @param args the command line parameters |
| 40 |
* @throws Exception on failure |
| 41 |
*/ |
| 42 |
public static void main(String... args) throws Exception { |
| 43 |
|
| 44 |
String file = "test.txt"; |
| 45 |
String cipher = "AES"; |
| 46 |
String filePassword = "password"; |
| 47 |
String compressionAlgorithm = "DEFLATE"; |
| 48 |
|
| 49 |
String url = "jdbc:h2:mem:test"; |
| 50 |
String user = "sa", dbPassword = "sa"; |
| 51 |
|
| 52 |
PrintWriter w = openScriptWriter(file, |
| 53 |
compressionAlgorithm, |
| 54 |
cipher, filePassword, "UTF-8"); |
| 55 |
w.println("create table test(id int primary key);"); |
| 56 |
w.println("insert into test select x from system_range(1, 10);"); |
| 57 |
w.close(); |
| 58 |
|
| 59 |
Class.forName("org.h2.Driver"); |
| 60 |
Connection conn = DriverManager.getConnection(url, user, dbPassword); |
| 61 |
RunScript.main( |
| 62 |
"-url", url, |
| 63 |
"-user", user, "-password", dbPassword, |
| 64 |
"-script", file, |
| 65 |
"-options", |
| 66 |
"compression", compressionAlgorithm, |
| 67 |
"cipher", cipher, |
| 68 |
"password", "'" + filePassword + "'" |
| 69 |
); |
| 70 |
Script.main( |
| 71 |
"-url", url, |
| 72 |
"-user", user, "-password", dbPassword, |
| 73 |
"-script", file, |
| 74 |
"-options", |
| 75 |
"compression", compressionAlgorithm, |
| 76 |
"cipher", cipher, "password", "'" + filePassword + "'" |
| 77 |
); |
| 78 |
conn.close(); |
| 79 |
|
| 80 |
LineNumberReader r = openScriptReader(file, |
| 81 |
compressionAlgorithm, |
| 82 |
cipher, filePassword, "UTF-8"); |
| 83 |
while (true) { |
| 84 |
String line = r.readLine(); |
| 85 |
if (line == null) { |
| 86 |
break; |
| 87 |
} |
| 88 |
System.out.println(line); |
| 89 |
} |
| 90 |
r.close(); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Open a script writer. |
| 96 |
* |
| 97 |
* @param fileName the file name (the file will be overwritten) |
| 98 |
* @param compressionAlgorithm the compression algorithm (uppercase) |
| 99 |
* @param cipher the encryption algorithm or null |
| 100 |
* @param password the encryption password |
| 101 |
* @param charset the character set (for example UTF-8) |
| 102 |
* @return the print writer |
| 103 |
* @throws IOException on failure |
| 104 |
*/ |
| 105 |
public static PrintWriter openScriptWriter(String fileName, |
| 106 |
String compressionAlgorithm, |
| 107 |
String cipher, String password, |
| 108 |
String charset) throws IOException { |
| 109 |
try { |
| 110 |
OutputStream out; |
| 111 |
if (cipher != null) { |
| 112 |
byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray()); |
| 113 |
FileUtils.delete(fileName); |
| 114 |
FileStore store = FileStore.open(null, fileName, "rw", cipher, key); |
| 115 |
store.init(); |
| 116 |
out = new FileStoreOutputStream(store, compressionAlgorithm); |
| 117 |
out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS); |
| 118 |
} else { |
| 119 |
out = FileUtils.newOutputStream(fileName, false); |
| 120 |
out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE); |
| 121 |
out = CompressTool.wrapOutputStream(out, |
| 122 |
compressionAlgorithm, "script.sql"); |
| 123 |
} |
| 124 |
return new PrintWriter(new OutputStreamWriter(out, charset)); |
| 125 |
} catch (Exception e) { |
| 126 |
throw new IOException(e.getMessage(), e); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Open a script reader. |
| 132 |
* |
| 133 |
* @param fileName the file name (the file will be overwritten) |
| 134 |
* @param compressionAlgorithm the compression algorithm (uppercase) |
| 135 |
* @param cipher the encryption algorithm or null |
| 136 |
* @param password the encryption password |
| 137 |
* @param charset the character set (for example UTF-8) |
| 138 |
* @return the script reader |
| 139 |
* @throws IOException on failure |
| 140 |
*/ |
| 141 |
public static LineNumberReader openScriptReader(String fileName, |
| 142 |
String compressionAlgorithm, |
| 143 |
String cipher, String password, |
| 144 |
String charset) throws IOException { |
| 145 |
try { |
| 146 |
InputStream in; |
| 147 |
if (cipher != null) { |
| 148 |
byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray()); |
| 149 |
FileStore store = FileStore.open(null, fileName, "rw", cipher, key); |
| 150 |
store.init(); |
| 151 |
in = new FileStoreInputStream(store, compressionAlgorithm != null, false); |
| 152 |
in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE_COMPRESS); |
| 153 |
} else { |
| 154 |
in = FileUtils.newInputStream(fileName); |
| 155 |
in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE); |
| 156 |
in = CompressTool.wrapInputStream(in, compressionAlgorithm, "script.sql"); |
| 157 |
if (in == null) { |
| 158 |
throw new IOException("Entry not found: script.sql in " + fileName); |
| 159 |
} |
| 160 |
} |
| 161 |
return new LineNumberReader(new InputStreamReader(in, charset)); |
| 162 |
} catch (Exception e) { |
| 163 |
throw new IOException(e.getMessage(), e); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
} |