以下是我如何使用截击下载zip文件:
package basavaraj.com.myapplicationtwo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.Volley;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements Response.Listener<byte[]>, ErrorListener {
EditText eText;
Button download, unzip;
boolean flag = false;
private static Random random = new Random(Calendar.getInstance().getTimeInMillis());
private ProgressDialog mProgressDialog;
String unzipLocation = Environment.getExternalStorageDirectory() + "/mycatalog";
String zipFile;
InputStreamVolleyRequest request;
int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
download = (Button) findViewById(R.id.download);
unzip = (Button) findViewById(R.id.unzip);
download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*DownloadMapAsync mew = new DownloadMapAsync();
mew.execute("https://docs.google.com/uc?export=download&id=0BwxIiPT46CGHMWFTX0o4bzMzYU0");*/
String mUrl="https://docs.google.com/uc?export=download&id=0BwxIiPT46CGHMWFTX0o4bzMzYU0";
request = new InputStreamVolleyRequest(Request.Method.GET, mUrl, MainActivity.this, MainActivity.this, null);
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(),
new HurlStack());
mRequestQueue.add(request);
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Downloading Zip File..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
});
unzip.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*if (flag) {*/
try {
unzip();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onResponse(byte[] response) {
HashMap<String, Object> map = new HashMap<String, Object>();
try {
if (response!=null) {
//Read file name from headers
String content =request.responseHeaders.get("Content-Disposition")
.toString();
StringTokenizer st = new StringTokenizer(content, "=");
/*String arrTag = st.toString();
String filename = arrTag;*/
String filename = content.split("'")[2];
filename = filename.replace(":", ".");
Log.d("DEBUG::RESUME FILE NAME", filename);
try{
long lenghtOfFile = response.length;
//covert reponse to input stream
InputStream input = new ByteArrayInputStream(response);
File path = Environment.getExternalStorageDirectory();
File file = new File(path, filename);
zipFile = file.toString();
map.put("resume_path", file.toString());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
mProgressDialog.dismiss();
}catch(IOException e){
e.printStackTrace();
}
}
} catch (Exception e) {
mProgressDialog.dismiss();
// TODO Auto-generated catch block
Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
e.printStackTrace();
}
}
@Override
public void onErrorResponse(VolleyError error) {
mProgressDialog.dismiss();
Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE. ERROR:: "+error.getMessage());
}
public void unzip() throws IOException {
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Please Wait...Extracting zip file ... ");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
new UnZipTask().execute(zipFile, unzipLocation);
}
private class UnZipTask extends AsyncTask<String, Void, Boolean> {
@SuppressWarnings("rawtypes")
@Override
protected Boolean doInBackground(String... params) {
String filePath = params[0];
String destinationPath = params[1];
File archive = new File(filePath);
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries();
e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, destinationPath);
}
UnzipUtil d = new UnzipUtil(zipFile, unzipLocation);
d.unzip();
} catch (Exception e) {
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
mProgressDialog.dismiss();
}
private void unzipEntry(ZipFile zipfile, ZipEntry entry,
String outputDir) throws IOException {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
// Log.v("", "Extracting: " + entry);
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
} finally {
outputStream.flush();
outputStream.close();
inputStream.close();
}
}
private void createDir(File dir) {
if (dir.exists()) {
return;
}
if (!dir.mkdirs()) {
throw new RuntimeException("Can not create dir " + dir);
}
}
}
}
UnzipUtil.java
package basavaraj.com.myapplicationtwo;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnzipUtil {
private String _zipFile;
private String _location;
public UnzipUtil(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
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(_location + ze.getName());
// for (int c = zin.read(); c != -1; c = zin.read()) {
// fout.write(c);
byte[] buffer = new byte[8192];
int len;
while ((len = zin.read(buffer)) != -1) {
fout.write(buffer, 0, len);
}
fout.close();
// }
zin.closeEntry();
// fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(f.isDirectory()) {
f.mkdirs();
}
}
}
InputStreamVolleyRequest.java
package basavaraj.com.myapplicationtwo;
/**
* Created by VISHAL on 4/5/2017.
*/
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import java.util.HashMap;
import java.util.Map;
public class InputStreamVolleyRequest extends Request<byte[]> {
private final Response.Listener<byte[]> mListener;
private Map<String, String> mParams;
//create a static map for directly accessing headers
public Map<String, String> responseHeaders;
public InputStreamVolleyRequest(int post, String mUrl, Response.Listener<byte[]> listener,
Response.ErrorListener errorListener, HashMap<String, String> params) {
// TODO Auto-generated constructor stub
super(post, mUrl, errorListener);
// this request would never use cache.
setShouldCache(false);
mListener = listener;
mParams = params;
}
@Override
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return mParams;
}
;
@Override
protected void deliverResponse(byte[] response) {
mListener.onResponse(response);
}
@Override
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
//Initialise local responseHeaders map with response headers received
responseHeaders = response.headers;
//Pass the response data here
return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));
}
}
我正在使用Python2.7、mechanize和beautifulsoup,如果有帮助,我可以使用urllib 我打算使用下面的代码来访问第二个表: 我猜class=“fe-form”是错误的,因为它不能工作,但是该表没有其他属性将它与其他表区分开来。所有表都有cellpadding=“0”cellspacing=“0”border=“0”width=“50%”。我想我不能使用find()函数。
我有我的网站在和我想下载一些文件从另一个域但没有得到下载和显示302移动临时错误。我使用cURL代码。
问题内容: 如何使用php将多个文件下载为zip文件? 问题答案: 您可以使用该类创建一个ZIP文件并将其流式传输到客户端。就像是: 并流式传输: 第二行强制浏览器向用户显示一个下载框,并提示名称filename.zip。第三行是可选的,但某些(主要是较旧的)浏览器在某些情况下会出现问题,而未指定内容大小。
问题内容: 两部分的问题。我正在尝试从互联网档案中下载多个已存档的Cory Doctorow播客。我的iTunes提要中未包含的旧版本。我已经编写了脚本,但是下载的文件格式不正确。 问题1-如何更改以下载zip mp3文件?问题2-将变量传递到URL的更好方法是什么? 该脚本是从这里改编的 问题答案: 这是我处理URL构建和下载的方式。我确保将文件命名为url的基本名称(后跟斜杠后的最后一位),并
我有下面的代码从mkyong得到,到本地的zip文件。但,我的要求是在服务器上压缩文件,并需要下载。谁能帮忙吗。 写入zipFiles的代码: 我可以在fileoutputstream这里提供什么?内容文件和导航文件是我从代码中创建的文件。
我有一个zip文件的下载链接。但我不知道如何从Python控制台使用wget或cURL将其放入我的下载文件夹(OSX)。 或 这些都不能下载该文件。