Android應用中如何對文件進行壓縮與解壓縮?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)公司服務項目包括新蔡網站建設、新蔡網站制作、新蔡網頁制作以及新蔡網絡營銷策劃等。多年來,我們專注于互聯(lián)網行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網行業(yè)的解決方案,新蔡網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到新蔡省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
使用場景
當我們在應用的Assets目錄中需要加入文件時,可以直接將源文件放入,但這樣會造成打包后的apk整體過大,此時就需要將放入的文件進行壓縮.又如當我們需要從服務器中下載文件時,如果下載源文件耗時又消耗流量,較大文件需要壓縮,可以使得傳輸效率大大提高.下面我們就學習下基本的文件壓縮和解壓縮.Java中提供了壓縮和解壓縮的輸入輸出流
public static void zip(String src,String dest) throwsIOException {
//定義壓縮輸出流
ZipOutputStreamout = null;
try {
//傳入源文件
File outFile= newFile(dest);
File fileOrDirectory= newFile(src);
//傳入壓縮輸出流
out = newZipOutputStream(newFileOutputStream(outFile));
//判斷是否是一個文件或目錄
//如果是文件則壓縮
if (fileOrDirectory.isFile()){
zipFileOrDirectory(out,fileOrDirectory, "");
} else {
//否則列出目錄中的所有文件遞歸進行壓縮
File[]entries = fileOrDirectory.listFiles();
for (int i= 0; i < entries.length;i++) {
zipFileOrDirectory(out,entries,"");
}
}
}catch(IOException ex) {
ex.printStackTrace();
}finally{
if (out!= null){
try {
out.close();
}catch(IOException ex) {
ex.printStackTrace();
}
}
}
}
private static void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory, String curPath)throwsIOException {
FileInputStreamin = null;
try {
//判斷目錄是否為null
if (!fileOrDirectory.isDirectory()){
byte[] buffer= new byte[4096];
int bytes_read;
in= newFileInputStream(fileOrDirectory);
//歸檔壓縮目錄
ZipEntryentry = newZipEntry(curPath + fileOrDirectory.getName());
//將壓縮目錄寫到輸出流中
out.putNextEntry(entry);
while ((bytes_read= in.read(buffer))!= -1) {
out.write(buffer,0, bytes_read);
}
out.closeEntry();
} else {
//列出目錄中的所有文件
File[]entries = fileOrDirectory.listFiles();
for (int i= 0; i < entries.length;i++) {
//遞歸壓縮
zipFileOrDirectory(out,entries,curPath + fileOrDirectory.getName()+ "/");
}
}
}catch(IOException ex) {
ex.printStackTrace();
}finally{
if (in!= null){
try {
in.close();
}catch(IOException ex) {
ex.printStackTrace();
}
}
}
}上述代碼存在問題,若文件壓縮后仍然很大怎么辦,換句話說文件壓縮率低也是問題,java中也專門對Linux提供了高壓縮率的輸入輸出流,其使用方法和上述代碼相似.高壓縮率輸入輸出流:(GZIPInputStream和GZIPOutputStream)
文件壓縮
public static void zip(File srcFile, File desFile)throwsIOException {
GZIPOutputStreamzos = null;
FileInputStreamfis = null;
try {
//創(chuàng)建壓縮輸出流,將目標文件傳入
zos = newGZIPOutputStream(newFileOutputStream(desFile));
//創(chuàng)建文件輸入流,將源文件傳入
fis = newFileInputStream(srcFile);
byte[] buffer= new byte[1024];
int len= -1;
//利用IO流寫入寫出的形式將源文件寫入到目標文件中進行壓縮
while ((len= (fis.read(buffer)))!= -1) {
zos.write(buffer,0, len);
}
}finally{
close(zos);
close(fis);
}
}文件解壓縮
public static void unZip(File srcFile,File desFile) throws IOException {
GZIPInputStream zis= null;
FileOutputStreamfos = null;
try {
//創(chuàng)建壓縮輸入流,傳入源文件
zis = new GZIPInputStream(newFileInputStream(srcFile));
//創(chuàng)建文件輸出流,傳入目標文件
fos = newFileOutputStream(desFile);
byte[] buffer= new byte[1024];
int len= -1;
//利用IO流寫入寫出的形式將壓縮源文件解壓到目標文件中
while ((len= (zis.read(buffer)))!= -1) {
fos.write(buffer,0, len);
}
}finally{
close(zis);
close(fos);
}
}看完上述內容,你們掌握Android應用中如何對文件進行壓縮與解壓縮的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當前題目:Android應用中如何對文件進行壓縮與解壓縮
當前網址:http://chinadenli.net/article36/ipcisg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、自適應網站、、品牌網站建設、網站制作、ChatGPT
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)