2012-11-16 102 views
0

我想要做的是加密和壓縮文件,並將它們存儲在設備的SD卡上。壓縮和加密文件。

我還沒有使用原始文件或壓縮之前,所以我不知道從哪裏開始。

在Android上可以做到嗎?我正在使用4.0.3,是否可以像1 GB文件夾一樣壓縮?或者我是否必須將它們分成可管理的塊?

任何想法?

回答

1
import java.io.*;import java.util.zip.*; 
public class Zip { 

    public static void main(String[] arg) 
    { 
    String[] source = new String[]{"C:/Users/MariaHussain/Desktop/hussain.java","C:/Users/MariaHussain/Desktop/aa.txt"}; 
    byte[] buf = new byte[1024]; 
    try { 
     String target = "C:/Users/MariaHussain/Desktop/target1.zip"; 
     ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target)); 
     for (int i=0; i<source.length; i++) { 
      FileInputStream in = new FileInputStream(source[i]); 

      // Add ZIP entry to output stream. 
      out.putNextEntry(new ZipEntry(source[i])); 

      // Transfer bytes from the file to the ZIP file 
      int len; 
      while ((len = in.read(buf)) > 0) { 
       out.write(buf, 0, len); 
      } 

      // Complete the entry 
      out.closeEntry(); 
      in.close(); 
     } 

     // Complete the ZIP file 
     out.close(); 
    } catch (IOException e) { 

    } 

    } 

} 
1

您可以使用ZipInputStreamZipOutput流讀取寫入Zip文件。 Java文檔頁面也有讀取和寫入的示例代碼。你可以使用android加密庫進行加密/解密。