2017-07-29 54 views
0

如何在android中壓縮特定文件?例如,我只想壓縮像video.mp4,music.mp3,word.docx,image.jpeg等手機存儲中的隨機文件。我試圖在這裏搜索相同的問題,他們總是說,試試這個鏈接Zipping Files with Android (Programmatically),但該頁面已經找不到。你有替代的鏈接嗎?如何以編程方式ZIP特定文件

預先感謝您!我很感激。

+0

請看看這個答案:https://stackoverflow.com/a/47154408/2101822 –

回答

2

看看ZipOutputStream

你打開一個新的FileOutputStream中寫一個文件,然後在一個ZipOutputStream寫一個ZIP。然後,爲每個要壓縮的文件創建ZipEntrys並寫入它們。不要忘記關閉ZipEntrys和流。

例如:

// Define output stream 
FileOutputStream fos = new FileOutputStream("zipname.zip"); 
ZipOutputStream zos = new ZipOutputStream(fos); 

// alway use a try catch block and close the zip in the finally 
try { 
    ZipEntry zipEntry = new ZipEntry("entryname.txt"); 
    zos.putNextEntry(zipEntry); 
    // write any content you like 
    zos.write("file content".getBytes()); 
    zos.closeEntry(); 
} 
catch (Exception e) { 
    // unable to write zip 
} 
finally { 
    zos.close(); 
} 

希望它幫助!

相關問題