當前位置:
首頁 > 知識 > google snapy 壓縮文件

google snapy 壓縮文件

引入 google snappy 包

github 上snappy地址 http://github.com/google/snappy


org.xerial.snappy
snappy-java
1.1.2.6

使用snappy壓縮文件

import java.io.ByteArrayOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.xerial.snappy.Snappy;

public class SnappyDemo {

public static void main(String[] args) throws Exception {
// 待壓縮的文件,可以是任意文件
RandomAccessFile raf = new RandomAccessFile("c:/花好月圓夜.mp4", "r");
//壓縮後保存的文件
RandomAccessFile w = new RandomAccessFile("c:/cas-server-3.5.2-master.snappy", "rw");
FileChannel channel = raf.getChannel;
ByteBuffer buffer = ByteBuffer.allocate(4096);
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
while (channel.read(buffer) > -1) {
buffer.flip ;

while(buffer.hasRemaining){
out.write( buffer.get );
}
buffer.clear ;
}
//壓縮內容寫入 文件,如果文件比較大 壓縮後的文件可以分多個文件保存,
// 例如hadoop 文件每個塊自定義為128M
w.write( Snappy.compress(out.toByteArray) );
raf.close;
w.close;
out.close;
}
}

使用snappy解壓縮

import java.io.ByteArrayOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.xerial.snappy.Snappy;

public class UnSnappyDemo {

public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("c:/cas-server-3.5.2-master.snappy", "r");
RandomAccessFile w = new RandomAccessFile("c:/cas-server-3.5.2-master.zip", "rw");
FileChannel channel = raf.getChannel;
ByteBuffer buffer = ByteBuffer.allocate(196);
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
while (channel.read(buffer) > -1){
buffer.flip ;
while(buffer.hasRemaining){
out.write( buffer.get );
}
buffer.clear ;
buffer = ByteBuffer.allocate(196);
}
w.write( Snappy.uncompress(out.toByteArray) );
raf.close;
w.close;
}
}

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

用JMX遠程監控Tomcat
Scrapy教程——博客園前3000名文章列表抓取
Vulkan Tutorial 14 Integration pipeline
數據應用達人之SQL基礎教程分享3-創建第一張資料庫表

TAG:達人科技 |

您可能感興趣

Spring Boot 配置文件 yml與properties
tempfile.NamedTemporaryFile創建臨時文件在windows沒有許可權打開
使用python將excel轉為lua文件
詳解Github的.gitignore忽略文件+.gitignore不生效解決+生產配置
Project Parfait將Photoshop文件轉換為代碼
Linux系統安裝Oracle,配置etc/security/limits.conf文件
tensorboard多個events文件顯示紊亂的解決辦法
Win10刪除需要trustedinstaller許可權的文件的方法
Spring Boot 文件上傳
Web Pages 文件
Avante Technology發布Emendo Cloud 3D列印文件自動分析和維修服務
乾貨:詳解 Tomcat 配置文件 server.xml
使用 YAML 文件創建 Kubernetes Deployment
讀取ClassPath下resource文件的正確姿勢
WordPress插件WooCommerce任意文件刪除漏洞分析
Eclipse編輯Spring配置文件xml時自動提示類class包名
理解 Linux 中的 /etc/services 文件
不打開文件獲取名稱。Excel VBA Application對象GetOpenFilename方法教程
iMessage漏洞致iPhone上任意文件讀取
Python使用pandas讀取Excel文件多個WorkSheet的數據並繪製柱狀圖和熱力圖