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

如何在android中压缩和解压png图像,android,android-emulator,Android,Android Emulator,您好,在我的应用程序中,当我单击zip按钮时,我需要压缩图像文件,当我单击unzip按钮时,我需要解压缩文件,我尝试使用下面的代码压缩图像,但我的问题是当我单击zip按钮时,zip文件正在创建,但在使用winzip软件的系统中,我尝试打开文件,但没有打开它的显示“它似乎不是一个有效的存档文件”我犯了一个错误,你能告诉我如何压缩和解压图像吗 公共类MainActivity扩展了活动{ /**在首次创建活动时调用*/ Button zip,unzip; String []s=new Strin

您好,在我的应用程序中,当我单击zip按钮时,我需要压缩图像文件,当我单击unzip按钮时,我需要解压缩文件,我尝试使用下面的代码压缩图像,但我的问题是当我单击zip按钮时,zip文件正在创建,但在使用winzip软件的系统中,我尝试打开文件,但没有打开它的显示“它似乎不是一个有效的存档文件”我犯了一个错误,你能告诉我如何压缩和解压图像吗

公共类MainActivity扩展了活动{ /**在首次创建活动时调用*/

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(); 
} 

   } 

     } 
按钮压缩、解压;
字符串[]s=新字符串[2];
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
zip=(按钮)findViewById(R.id.button1);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
//TODO自动生成的方法存根
s[0]=“/sdcard/saved_images/Main.png”;
//s[1]=“/sdcard/Physics_Lab/Stefans_Law/stefan_Law.txt”;//第二个文件的路径
压缩c=新压缩,“/sdcard/saved_images/stefen.zip”);
c、 zip();
}
});
}
}
公共类压缩{
专用静态最终整数缓冲区=80000;
私有字符串[]_文件;
私有字符串_zipFile;
公共压缩(字符串[]文件,字符串zipFile){
_文件=文件;
_zipFile=zipFile;
} 
public void zip(){
试试{
BufferedInputStream原点=null;
FileOutputStream dest=新的FileOutputStream(_zipFile);
ZipOutputStream out=新的zipoutpstream(新的缓冲输出流(dest));
字节数据[]=新字节[缓冲区];
对于(int i=0;i<_files.length;i++){
Log.d(“添加:”,_文件[i]);
Log.v(“压缩”,“添加:”+_文件[i]);
FileInputStream fi=新的FileInputStream(_files[i]);
原点=新的BufferedInputStream(fi,缓冲区);
ZipEntry entry=newzipentry(_文件[i]。子字符串(_文件[i]。lastIndexOf(“/”+1));
外出、外出(进入);
整数计数;
而((count=origin.read(数据,0,缓冲区))!=-1{
输出。写入(数据,0,计数);
} 
origin.close();
} 
out.close();
}捕获(例外e){
e、 printStackTrace();
} 
} 
} 

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


您可以在Java中使用ZipoutStream

您可以通过follow函数创建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,我创建了压缩类,我只是复制了所有代码,在zip onclick中我没有更改代码,现在也是同一个prblm,我无法使用winzip Sofware打开创建的zip文件。在我的zip onclcik代码中可以看到一次,可能是在这个代码问题上,我想你一定是搞错了。我已经成功地使用了这个代码。@Sandeep两个链接都是dea实际上还有其他类似于zip和解压的项目,但我仍然认为ZipoutStream很好:Dok,但是使用上面的代码zip文件正在创建,但它没有打开哦,对不起,我太快了,犯了一个错误,请看编辑过的版本我试着使用下面的链接,我没有尝试你的代码…但它也会工作。不管怎样,谢谢JeffLee
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();
 }