「Java」クラスGZIPOutputStreamのサンプル
説明
public class GZIPOutputStream extends DeflaterOutputStream
このクラスは、GZIPファイル形式で圧縮されたデータを書き込むためのストリーム・フィルタを実装します。
使用例
package com.arkgame.study.javlesson;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPOutputStream;
public class GZIPOutDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "argame";
byte[] target = str.getBytes("UTF-8");
System.out.println("圧縮前");
for (byte tg : target) {
System.out.print(tg + "*");
}
try {
gzipFunc(target);
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
public static byte[] gzipFunc(byte[] src) throws IOException {
byte res[] = null;
try (ByteArrayOutputStream ba = new ByteArrayOutputStream();) {
try (GZIPOutputStream gzos = new GZIPOutputStream(ba)) {
gzos.write(src);
}
res = ba.toByteArray();
}
System.out.println("\n圧縮後");
for (byte bb : res) {
System.out.print(bb + "*");
}
return res;
}
}
package com.arkgame.study.javlesson;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPOutputStream;
public class GZIPOutDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "argame";
byte[] target = str.getBytes("UTF-8");
System.out.println("圧縮前");
for (byte tg : target) {
System.out.print(tg + "*");
}
try {
gzipFunc(target);
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
public static byte[] gzipFunc(byte[] src) throws IOException {
byte res[] = null;
try (ByteArrayOutputStream ba = new ByteArrayOutputStream();) {
try (GZIPOutputStream gzos = new GZIPOutputStream(ba)) {
gzos.write(src);
}
res = ba.toByteArray();
}
System.out.println("\n圧縮後");
for (byte bb : res) {
System.out.print(bb + "*");
}
return res;
}
}
package com.arkgame.study.javlesson; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.zip.GZIPOutputStream; public class GZIPOutDemo { public static void main(String[] args) throws UnsupportedEncodingException { String str = "argame"; byte[] target = str.getBytes("UTF-8"); System.out.println("圧縮前"); for (byte tg : target) { System.out.print(tg + "*"); } try { gzipFunc(target); } catch (IOException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } public static byte[] gzipFunc(byte[] src) throws IOException { byte res[] = null; try (ByteArrayOutputStream ba = new ByteArrayOutputStream();) { try (GZIPOutputStream gzos = new GZIPOutputStream(ba)) { gzos.write(src); } res = ba.toByteArray(); } System.out.println("\n圧縮後"); for (byte bb : res) { System.out.print(bb + "*"); } return res; } }
圧縮前
97*114*103*97*109*101*
圧縮後
31*-117*8*0*0*0*0*0*0*0*75*44*74*79*-52*77*5*0*99*32*23*-13*6*0*0*0*