2017-07-29 31 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(); 
} 

希望它帮助!

相关问题