如何在android中压缩和解压缩png图像

时间:2013-05-21 06:37:24

标签: android android-emulator

嗨,在我的应用程序,当我点击zip按钮我需要压缩图像文件,当我点击解压缩按钮我需要解压缩文件,我尝试使用下面的代码来压缩图像,但我的问题是当我点击zip按钮zip文件正在创建,但在系统之后使用winzip软件我尝试打开文件,但它没有打开它显示“它似乎没有一个有效的存档有效文件”我错了你可以让我如何压缩和解压缩图像

公共类MainActivity扩展Activity {     /** 在第一次创建活动时调用。 * /

Button zip,unzip; 
  String []s=new String[2];   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    zip=(Button)findViewById(R.id.button1);
    zip.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            s[0]="/sdcard/saved_images/Main.png";    
            //s[1]="/sdcard/Physics_Lab/Stefans_Law/stefan_law.txt"; // path of the second file
            Compress c =new Compress(s,"/sdcard/saved_images/stefen.zip");   
            c.zip();    
        }
    });

    }
  }
    public class Compress { 
      private static final int BUFFER = 80000; 

      private String[] _files; 
        private String _zipFile; 

   public Compress(String[] files, String zipFile) { 
        _files = files; 
     _zipFile = zipFile; 

    } 

   public void zip() { 
    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(); 
} 

   } 

     } 

2 个答案:

答案 0 :(得分:1)

您可以查看这两个教程来压缩和解压缩文件

Android Zip file

Android Unzip file

答案 1 :(得分:0)

您可以在Java中使用ZipOutputStream

See API Document Here

您可以按照以下功能

创建zip文件
 OutputStream os = ...
 ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
 try {
     for (int i = 0; i < fileCount; ++i) {
         String filename = ...
         byte[] bytes = ...
         ZipEntry entry = new ZipEntry(filename);
         zos.putNextEntry(entry);
         zos.write(bytes);
         zos.closeEntry();
     }
 } finally {
     zos.close();
 }

对于打开zip文件,您可以使用ZipInputStream Api Document here

InputStream is = ...
 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
 try {
     ZipEntry ze;
     while ((ze = zis.getNextEntry()) != null) {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         byte[] buffer = new byte[1024];
         int count;
         while ((count = zis.read(buffer)) != -1) {
             baos.write(buffer, 0, count);
         }
         String filename = ze.getName();
         byte[] bytes = baos.toByteArray();
         // do something with 'filename' and 'bytes'...
     }
 } finally {
     zis.close();
 }