如何在安卓设备上进行PNG图片的压缩和解压缩

3

你好,在我的应用程序中,当我点击压缩按钮时,我需要压缩图像文件,而当我点击解压缩按钮时,我需要解压缩文件。我尝试使用下面的代码来压缩图像,但是我的问题是当我点击压缩按钮时,压缩文件被创建了,但在使用WinZip软件打开文件后,它无法打开,显示“似乎不是有效的存档文件”,请问我错在哪里,能否告诉我如何压缩和解压缩图像?

public class MainActivity extends 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个回答

1

好的,对于压缩,我创建了一个压缩类,只是复制了所有代码,在zip onclick中我没有改变代码,现在还是同样的问题,我无法使用WinZip软件打开创建的zip文件。 - user2401554
我在我的zip onclcik代码中只能看到一次,可能是这个代码的问题。 - user2401554
不,你一定是犯了某些错误。我已经成功地使用了这段代码。 - Sandeep
@Sandeep 两个链接都失效了。 - Moritz Schmidt

0
你可以在Java中使用ZipOutputStream 查看API文档 您可以通过以下函数创建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文档

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

实际上还有其他的压缩和解压项目,比如 https://truezip.java.net/ 但我仍然认为 ZipOutputStream 很好 :D - Jeff Lee
好的,但是使用上面的代码创建了压缩文件,但它无法打开。 - user2401554
抱歉,我打字太快了,出现了错误,请查看编辑后的版本。 - Jeff Lee
我尝试使用以下链接,我没有尝试过您的代码...但它也会起作用..无论如何,谢谢JeffLee。 - user2401554

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接