2012-02-10 18 views

答えて

4

次のコードは、私の問題を解決しました。

public class makeZip { 
    static final int BUFFER = 2048; 

    ZipOutputStream out; 
    byte data[]; 

    public makeZip(String name) { 
     FileOutputStream dest=null; 
     try { 
      dest = new FileOutputStream(name); 
     } 
     catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     out = new ZipOutputStream(new BufferedOutputStream(dest)); 
     data = new byte[BUFFER]; 
    } 

    public void addZipFile (String name) { 
     Log.v("addFile", "Adding: "); 
     FileInputStream fi=null; 
     try { 
      fi = new FileInputStream(name); 
      Log.v("addFile", "Adding: "); 
     } 
     catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Log.v("atch", "Adding: "); 
     } 
     BufferedInputStream origin = new BufferedInputStream(fi, BUFFER); 
     ZipEntry entry = new ZipEntry(name); 
     try { 
      out.putNextEntry(entry); 
      Log.v("put", "Adding: "); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     int count; 
     try { 
      while((count = origin.read(data, 0, BUFFER)) != -1) { 
       out.write(data, 0, count); 
       //Log.v("Write", "Adding: "+origin.read(data, 0, BUFFER)); 
      } 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Log.v("catch", "Adding: "); 
     } 
     try { 
      origin.close(); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public void closeZip() { 
     try { 
      out.close(); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
0

を支援するAndroidのSDK GZIPのAPIを、持っているあなたができることにより、圧縮されたデータを読み書きします。 public class GZIPOutputStreamを参照してください。

0

次のコードを使用しますが、SDカードに保存する場合は、出力ストリームではなくファイル出力ストリームを使用してください。

private String compressData(String uncompressedData) { 
    String compressedData = null; 
    try { 
     if (uncompressedData.length() > 200) { 
      // Perform Compression. 
      byte[] originalBytes = uncompressedData.getBytes(); 

      Deflater deflater = new Deflater(); 
      deflater.setInput(originalBytes); 
      deflater.finish(); 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      byte[] buf = new byte[8192]; 
      while (!deflater.finished()) { 
       int byteCount = deflater.deflate(buf); 
       baos.write(buf, 0, byteCount); 
      } 
      deflater.end(); 

      byte[] compressedBytes = baos.toByteArray(); 

      compressedData = new String(compressedBytes, 0, compressedBytes.length); 
     } 
    } 
    catch (Exception e) { 
     compressedData = null; 
    } 
    return compressedData; 
} 
0

あなたがSDカード内のフォルダを持っていて、それのzipファイルを作成したい場合は、単にコピーして貼り付け、このコードをプロジェクトに、それはあなたのzipフォルダを与えるだろう。このコードは、ネストされたフォルダが内部に存在しないファイルのみを含むフォルダのzipを作成します。あなたは自分のためにさらに修正することができます。

String []s=new String[2]; //declare an array for storing the files i.e the path of your source files 
    s[0]="/mnt/sdcard/Wallpaper/pic.jpg"; //Type the path of the files in here 
    s[1]="/mnt/sdcard/Wallpaper/Final.pdf"; // path of the second file 

    zip((s,"/mnt/sdcard/MyZipFolder.zip"); //call the zip function 


public void zip(String[] files, String zipFile) 
{ 
    private String[] _files= files; 
    private String _zipFile= zipFile; 

try { 
    BufferedInputStream origin = null; 
    FileOutputStream dest = new FileOutputStream(_zipFile); 

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 

    byte data[] = new byte[BUFFER]; 

    for(int i=0; i < _files.length; i++) { 
     Log.d("add:",_files[i]); 
    Log.v("Compress", "Adding: " + _files[i]); 
    FileInputStream fi = new FileInputStream(_files[i]); 
    origin = new BufferedInputStream(fi, BUFFER); 
    ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
    out.putNextEntry(entry); 
    int count; 
    while ((count = origin.read(data, 0, BUFFER)) != -1) { 
     out.write(data, 0, count); 
    } 
    origin.close(); 
    } 

    out.close(); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 

}

も、このコード

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> 
を使用してアンドロイドのmanifest.xmlに権限を追加します
関連する問題