【问题标题】:How to zip and unzip png images in android如何在android中压缩和解压缩png图像
【发布时间】:2013-05-21 06:37:23
【问题描述】:

嗨,在我的应用程序中,当我单击 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(); 
} 

   } 

     } 

【问题讨论】:

    标签: android android-emulator


    【解决方案1】:

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

    Android Zip file

    Android Unzip file

    【讨论】:

    • 好的,对于压缩,我创建了压缩类,我只是复制了所有代码,并且在 zip onclick 中我没有更改代码,现在也是相同的 prblm,我无法使用 winzip 软件打开创建的 zip 文件
    • 可以在我的 zip onclcik 代码中看到一次,我认为可能是这个代码问题
    • 不,你一定是做错了什么。我已成功使用此代码。
    • @Sandeep 两个链接都失效了
    【解决方案2】:

    您可以在 Java 中使用 ZipOutputStream

    See API Document Here

    您可以通过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 文件,你可以使用 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();
     }
    

    【讨论】:

    • 其实还有其他的压缩和解压项目,比如truezip.java.net 但我还是觉得 ZipOutputStream 不错 :D
    • 好的,但是使用上面的代码压缩文件正在创建但它没有打开
    • 哦,,对不起,我打的太快,打错了,请看修改后的版本
    • 我试过使用下面的链接,我没有尝试你的代码......但它也可以工作......无论如何谢谢 JeffLee
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多