嗨,在我的应用程序中,当我单击 zip 按钮时,我需要压缩图像文件,当我单击解压缩按钮时,我需要解压缩文件,我尝试使用下面的代码压缩图像,但我的问题是当我单击 zip 按钮时正在创建 zip 文件,但是在那之后在使用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();
}
}
}