ラベル ApacheCommonsCompress の投稿を表示しています。 すべての投稿を表示
ラベル ApacheCommonsCompress の投稿を表示しています。 すべての投稿を表示

2010年9月20日月曜日

groovyとApache Commons Compressでgzipファイルを解凍する

groovyとApache Commons Compressでgzipファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.gzip.*

// tgzファイルオープン
fn = "test.tgz"
fis = new FileInputStream(fn)
// 書き込みファイルオープン
fos = new FileOutputStream(
".\\out\\" +
GzipUtils.getUncompressedFilename(fn)
)
gcis = new GzipCompressorInputStream(fis)
// 解凍
buf = new byte[8192]
int rs = -1;
while( (rs = gcis.read(buf)) != -1 ){
fos.write(buf, 0, rs)
}
gcis.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月19日日曜日

groovyとApache Commons Compressでgzipファイルに圧縮する

groovyとApache Commons Compressでgzipファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.gzip.*

// 読み込みファイルオープン
fn = "test.tar"
fis = new FileInputStream(fn)
// tgzファイルオープン
fos = new FileOutputStream(
GzipUtils.getCompressedFilename(fn)
)
gcos = new GzipCompressorOutputStream(fos)
// 圧縮
buf = new byte[8192]
int rs = -1;
while( (rs = fis.read(buf)) != -1 ){
gcos.write(buf, 0, rs)
}
gcos.flush()
gcos.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月18日土曜日

groovyとApache Commons Compressでbzip2ファイルを解凍する

groovyとApache Commons Compressでbzip2ファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.bzip2.*

// bz2ファイルオープン
fn = "test.tar.bz2"
fis = new FileInputStream(fn)
// 書き込みファイルオープン
fos = new FileOutputStream(
".\\out\\" +
BZip2Utils.getUncompressedFilename(fn)
)
bz2cis = new BZip2CompressorInputStream(fis)
// 解凍
buf = new byte[8192]
int rs = -1;
while( (rs = bz2cis.read(buf)) != -1 ){
fos.write(buf, 0, rs)
}
bz2cis.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月17日金曜日

groovyとApache Commons Compressでbzip2ファイルに圧縮する

groovyとApache Commons Compressでbzip2ファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.compressors.bzip2.*

// 読み込みファイルオープン
fn = "test.tar"
fis = new FileInputStream(fn)
// bz2ファイルオープン
fos = new FileOutputStream(
BZip2Utils.getCompressedFilename(fn)
)
bz2cos = new BZip2CompressorOutputStream(fos, 8)
// 圧縮
buf = new byte[8192]
int rs = -1;
while( (rs = fis.read(buf)) != -1 ){
bz2cos.write(buf, 0, rs)
}
bz2cos.finish()
bz2cos.flush()
bz2cos.close()
fos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月16日木曜日

groovyとoracleでサーバのホスト・マシン名を表示する

groovyとoracleでサーバのホスト・マシン名を表示するには、以下のコードを実行します。

import groovy.sql.Sql

sql = Sql.newInstance(
"jdbc:oracle:thin:@localhost:1521:orcl",
"scott",
"tiger",
"oracle.jdbc.driver.OracleDriver")

query = """
select sys_context('USERENV', 'SERVER_HOST') as sc from dual
"""

// サーバのホスト・マシン名を表示する
sql.eachRow(query){
println("${it.sc}")
}


動作環境
groovy 1.7.4, JDK6 Update21, Oracle11g R2

2010年9月15日水曜日

groovyとApache Commons Compressでzipファイルを解凍する

groovyとApache Commons Compressでzipファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.zip.*
import org.apache.commons.compress.utils.*

is = new BufferedInputStream(new FileInputStream("test.zip"))
zais = new ZipArchiveInputStream(is, "Windows-31J", true)
dest = ".\\out\\"
while((ent = zais.getNextZipEntry()) != null){
println dest + ent.getName()
df = new File(dest + ent.getName())
if( ent.isDirectory() ){
df.mkdirs()
} else {
if( df.getParentFile().exists() == false ){
df.getParentFile().mkdirs()
}
OutputStream out = new FileOutputStream(df)
IOUtils.copy(zais, out, 8192)
out.close()
}
}
zais.close()
is.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月14日火曜日

groovyとApache Commons Compressでzipファイルに圧縮する

groovyとApache Commons Compressでzipファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.zip.*
import org.apache.commons.compress.utils.*

def writeEntries = { aos, base, path ->
if( path.isDirectory() ){
def children = path.listFiles()
if( children.length == 0 ){
// ディレクトリ追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("creating...:" + fn)
ent = new ZipArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
aos.closeArchiveEntry()
} else {
for(child in path.listFiles()){
call(aos, base, child)
}
}
} else {
// ファイル追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("adding...:" + fn)
ent = aos.createArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
IOUtils.copy(new FileInputStream(path), aos, 8192)
aos.closeArchiveEntry()
}
}

zaos = new ZipArchiveOutputStream(new File("test.zip"))
zaos.setEncoding("Windows-31J")
basedir = new File("testdir")
writeEntries(zaos, basedir, basedir)
zaos.finish()
zaos.flush()
zaos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月11日土曜日

groovyとApache Commons Compressでtarファイルを解凍する

groovyとApache Commons Compressでtarファイルを解凍するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.tar.*
import org.apache.commons.compress.utils.*

is = new BufferedInputStream(new FileInputStream("test.tar"))
tais = new TarArchiveInputStream(is, 8192)
dest = ".\\out\\"
while((ent = tais.getNextTarEntry()) != null){
println dest + ent.getName()
df = new File(dest + ent.getName())
if( ent.isDirectory() ){
df.mkdirs()
} else {
if( df.getParentFile().exists() == false ){
df.getParentFile().mkdirs()
}
OutputStream out = new FileOutputStream(df)
IOUtils.copy(tais, out, 8192)
out.close()
}
}
tais.close()
is.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1

2010年9月10日金曜日

groovyとApache Commons Compressでtarファイルに圧縮する

groovyとApache Commons Compressでtarファイルに圧縮するには、以下のコードを実行します。

import org.apache.commons.compress.archivers.tar.*
import org.apache.commons.compress.utils.*

def writeEntries = { aos, base, path ->
if( path.isDirectory() ){
def children = path.listFiles()
if( children.length == 0 ){
// ディレクトリ追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("creating...:" + fn)
ent = new TarArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
aos.closeArchiveEntry()
} else {
for(child in path.listFiles()){
call(aos, base, child)
}
}
} else {
// ファイル追加
def fn = path.getAbsolutePath().substring(
base.getAbsolutePath().length()+1).
replace(File.separator,'/')
println("adding...:" + fn)
ent = aos.createArchiveEntry(path, fn)
aos.putArchiveEntry(ent)
IOUtils.copy(new FileInputStream(path), aos, 8192)
aos.closeArchiveEntry()
}
}

taos = new TarArchiveOutputStream(
new FileOutputStream("test.tar"), 8192)
basedir = new File("testdir")
writeEntries(taos, basedir, basedir)
taos.finish()
taos.flush()
taos.close()


動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Compress 1.1