我想做一个项目来下载文件和提取文件。我尝试修复它很长时间。请查看我的代码并帮助我,或者有人告诉我如何下载文件和解压 zip 文件。在文件“download.zip”中包含 5 个视频文件。我使用Sreedev R 的Class Decompress
public class MainActivity extends AppCompatActivity {
private static String dirPath, dirPath2;
final String URL1 = "http://webmaster.com/01/defualt.zip";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dirPath = Utils.getRootDirPath(getApplicationContext());
dirPath2 = Utils.getRootDirPath(getApplicationContext())+"Unzip";
init();
onClickListenerOne();
Decompress unzip = new Decompress(dirPath+"/download.zip",dirPath2);
unzip.unzip();
}
解压类
public class Decompress {
private String zip;
private String loc;
public Decompress(String zipFile, String location) {
zip = zipFile;
loc = location;
dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(zip);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(loc + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void dirChecker(String dir) {
File f = new File(loc + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
森林海
相关分类