现在我需要下载zip文件并将其解压缩到Android中的固定SD卡文件中。我已经可以下载文件了,但我无法解压缩zip文件。我发现的问题是zipinputstream
为空。
下面的代码在java编程中都成功了,我不明白为什么这段代码不能在android中运行。
public void getZipFiles(String filename, String destinationname){
try{
Log.i("filename",filename);
Log.i("destinationname",destinationname);
File d = new File(destinationname);
if(!d.isDirectory()){
d.mkdir();
}
byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
zipinputstream = new ZipInputStream(
new FileInputStream(new File(filename)));
ZipEntry zipentry;
Log.i("read", ""+zipinputstream.read());
zipentry = zipinputstream.getNextEntry();
while (zipentry != null){
//for each entry to be extracted
String entryName = zipentry.getName();
System.out.println("entryname "+entryName);
int n;
FileOutputStream fileoutputstream;
fileoutputstream = new FileOutputStream(destinationname+entryName);
while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
fileoutputstream.write(buf, 0, n);
fileoutputstream.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}//while
zipinputstream.close();
}catch (IOException e){
e.printStackTrace();
System.out.println("!"+e.toString());
Log.e("err", e.toString());
}
}
这是我的php下载文件代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if($_GET['f']!=null){
$file=$_GET['f'];//file name
$url="http://58.176.4.199/"; //path
/* $num=date("Ymds");
$num =1;*/
header("Content-type:application");
header("Content-Disposition: attachment; filename=".$file);
//readfile($url.str_replace("@","",$file));
readfile($file);
exit(0);
}else{
echo "No file....";
}
?>
</body>
</html>