/** * 下载zip文件直接解压 * * @param context * @param dataUrl * @return */ public static boolean downloadAndUnzip(Context context, String dataUrl, String outPath) { InputStream is = null; try { URL url = new URL(dataUrl); // URL url = new URL(urlAddParams(context, dataUrl)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.addRequestProperty("User-Agent-YN", DeviceUtil.getDeviceInfoForUserAgentYN()); is = conn.getInputStream(); String fileName = getFileNameForUrl(dataUrl); String zipFileName; int index = fileName.indexOf("."); if (index != -1) { zipFileName = fileName.substring(0, index); } else { zipFileName = fileName; } return unZip(is, outPath, zipFileName); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
private static boolean doDownload(Context context, String imageUrl, File outFile) { URL url = null; InputStream is = null; FileOutputStream fos = null; try { url = new URL(imageUrl); // url = new URL(urlAddParams(context, imageUrl)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.addRequestProperty("User-Agent-YN", DeviceUtil.getDeviceInfoForUserAgentYN()); is = conn.getInputStream(); fos = new FileOutputStream(outFile); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) > 0) { fos.write(buf, 0, len); } fos.flush(); conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; }