Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
523a345d
提交
523a345d
authored
10月 09, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
A new sample application that shows how to manually create an encrypted and compressed script file.
上级
88e7a4c0
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
164 行增加
和
0 行删除
+164
-0
CreateScriptFile.java
h2/src/test/org/h2/samples/CreateScriptFile.java
+164
-0
没有找到文件。
h2/src/test/org/h2/samples/CreateScriptFile.java
0 → 100644
浏览文件 @
523a345d
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
samples
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.LineNumberReader
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.io.PrintWriter
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
org.h2.engine.Constants
;
import
org.h2.security.SHA256
;
import
org.h2.store.FileStore
;
import
org.h2.store.FileStoreInputStream
;
import
org.h2.store.FileStoreOutputStream
;
import
org.h2.tools.CompressTool
;
import
org.h2.tools.RunScript
;
import
org.h2.tools.Script
;
import
org.h2.util.IOUtils
;
/**
* This sample application shows how to manually
* create an encrypted and compressed script file.
*/
public
class
CreateScriptFile
{
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
public
static
void
main
(
String
...
args
)
throws
Exception
{
String
file
=
"test.txt"
;
String
cipher
=
"AES"
;
String
filePassword
=
"password"
;
String
compressionAlgorithm
=
"DEFLATE"
;
String
url
=
"jdbc:h2:mem:test"
;
String
user
=
"sa"
,
dbPassword
=
"sa"
;
PrintWriter
w
=
openScriptWriter
(
file
,
compressionAlgorithm
,
cipher
,
filePassword
,
"UTF-8"
);
w
.
println
(
"create table test(id int primary key);"
);
w
.
println
(
"insert into test select x from system_range(1, 10);"
);
w
.
close
();
Class
.
forName
(
"org.h2.Driver"
);
Connection
conn
=
DriverManager
.
getConnection
(
url
,
user
,
dbPassword
);
RunScript
.
main
(
"-url"
,
url
,
"-user"
,
user
,
"-password"
,
dbPassword
,
"-script"
,
file
,
"-options"
,
"compression"
,
compressionAlgorithm
,
"cipher"
,
cipher
,
"password"
,
"'"
+
filePassword
+
"'"
);
Script
.
main
(
"-url"
,
url
,
"-user"
,
user
,
"-password"
,
dbPassword
,
"-script"
,
file
,
"-options"
,
"compression"
,
compressionAlgorithm
,
"cipher"
,
cipher
,
"password"
,
"'"
+
filePassword
+
"'"
);
conn
.
close
();
LineNumberReader
r
=
openScriptReader
(
file
,
compressionAlgorithm
,
cipher
,
filePassword
,
"UTF-8"
);
while
(
true
)
{
String
line
=
r
.
readLine
();
if
(
line
==
null
)
{
break
;
}
System
.
out
.
println
(
line
);
}
r
.
close
();
}
/**
* Open a script writer.
*
* @param fileName the file name (the file will be overwritten)
* @param compressionAlgorithm the compression algorithm (uppercase)
* @param cipher the encryption algorithm or null
* @param password the encryption password
* @param charset the character set (for example UTF-8)
* @return the print writer
*/
public
static
PrintWriter
openScriptWriter
(
String
fileName
,
String
compressionAlgorithm
,
String
cipher
,
String
password
,
String
charset
)
throws
IOException
{
try
{
OutputStream
out
;
if
(
cipher
!=
null
)
{
byte
[]
key
=
new
SHA256
().
getKeyPasswordHash
(
"script"
,
password
.
toCharArray
());
IOUtils
.
delete
(
fileName
);
FileStore
store
=
FileStore
.
open
(
null
,
fileName
,
"rw"
,
cipher
,
key
);
store
.
init
();
out
=
new
FileStoreOutputStream
(
store
,
null
,
compressionAlgorithm
);
out
=
new
BufferedOutputStream
(
out
,
Constants
.
IO_BUFFER_SIZE_COMPRESS
);
}
else
{
out
=
IOUtils
.
openFileOutputStream
(
fileName
,
false
);
out
=
new
BufferedOutputStream
(
out
,
Constants
.
IO_BUFFER_SIZE
);
out
=
CompressTool
.
wrapOutputStream
(
out
,
compressionAlgorithm
,
"script.sql"
);
}
return
new
PrintWriter
(
new
OutputStreamWriter
(
out
,
charset
));
}
catch
(
Exception
e
)
{
throw
new
IOException
(
e
);
}
}
/**
* Open a script reader.
*
* @param fileName the file name (the file will be overwritten)
* @param compressionAlgorithm the compression algorithm (uppercase)
* @param cipher the encryption algorithm or null
* @param password the encryption password
* @param charset the character set (for example UTF-8)
* @return the script reader
*/
public
static
LineNumberReader
openScriptReader
(
String
fileName
,
String
compressionAlgorithm
,
String
cipher
,
String
password
,
String
charset
)
throws
IOException
{
try
{
InputStream
in
;
if
(
cipher
!=
null
)
{
byte
[]
key
=
new
SHA256
().
getKeyPasswordHash
(
"script"
,
password
.
toCharArray
());
FileStore
store
=
FileStore
.
open
(
null
,
fileName
,
"rw"
,
cipher
,
key
);
store
.
init
();
in
=
new
FileStoreInputStream
(
store
,
null
,
compressionAlgorithm
!=
null
,
false
);
in
=
new
BufferedInputStream
(
in
,
Constants
.
IO_BUFFER_SIZE_COMPRESS
);
}
else
{
in
=
IOUtils
.
openFileInputStream
(
fileName
);
in
=
new
BufferedInputStream
(
in
,
Constants
.
IO_BUFFER_SIZE
);
in
=
CompressTool
.
wrapInputStream
(
in
,
compressionAlgorithm
,
"script.sql"
);
if
(
in
==
null
)
{
throw
new
IOException
(
"Entry not found: script.sql in "
+
fileName
);
}
}
return
new
LineNumberReader
(
new
InputStreamReader
(
in
,
charset
));
}
catch
(
Exception
e
)
{
throw
new
IOException
(
e
);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论