java 壓縮和解壓lzo文件

1、依賴

<dependency>
	<groupId>org.anarres.lzo</groupId>
	<artifactId>lzo-core</artifactId>
	<version>1.0.0</version>
</dependency>
<dependency>
<span style="white-space:pre">	</span><groupId>org.anarres.lzo</groupId>
<span style="white-space:pre">	</span><artifactId>lzo-hadoop</artifactId>
<span style="white-space:pre">	</span><version>1.0.0</version>
</dependency>
<dependency>
	<groupId>org.apache.hadoop</groupId>
	<artifactId>hadoop-core</artifactId>
	<version>1.0.3</version>
</dependency>
如果在eclipse裏無法用maven插件下載這些依賴包,則需要手動將這三個包安裝到本地maven庫中,手動安裝請參考maven手動安裝jar及源碼
2、測試代碼

(1)壓縮

OutputStream out = new FileOutputStream(new File("D:\\log\\123.lzo"));
LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(algorithm, null);
LzoOutputStream stream = new LzoOutputStream(out, compressor, 256);
stream.write("我是中國人".getBytes("UTF-8"));
stream.close();
(2)解壓
<span style="white-space:pre">	</span>InputStream in = new FileInputStream(new File("D:\\log\\123.lzo"));
    	LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
    	LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
    	LzoInputStream stream = new LzoInputStream(in, decompressor);
    	OutputStream outputStream = new FileOutputStream(new File("D:\\log\\data.txt"));
    	int read = 0;
		byte[] bytes = new byte[1024];

		while ((read = stream.read(bytes)) != -1) {
			outputStream.write(bytes, 0, read);
		}
		outputStream.close();
    	stream.close();


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章