【发布时间】:2011-08-26 14:15:23
【问题描述】:
我有应用程序从 url 下载 .zip 文件并将每个文件从 .zip 文件转换为字节数组。目前我能够下载文件读取 .zip 文件并将整个 .zip 文件转换为字节,但在将 .zip 中的每个文件转换为字节数组时遇到了麻烦。任何帮助将不胜感激。我在下面附上了我的代码:
try {
URL url = new URL(Url);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
InputStream inputStream = urlConnection.getInputStream();
dis = new DataInputStream(new BufferedInputStream(inputStream));
System.out.println("INput connection done>>>>>");
zis = new ZipInputStream(new BufferedInputStream(dis));
String targetFolder="/sdcard/";
System.out.println("zip available is"+zis.available());
int extracted = 0;
while ((entry = zis.getNextEntry()) != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
String filename = entry.getName();
System.out.println("File name is>>"+filename);
byte[] bytes = baos.toByteArray();
System.out.println("Bytes is >>>>"+bytes.toString());
// do something with 'filename' and 'bytes'...
zis.closeEntry();
extracted ++;
}
zis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
我不明白你有什么问题。您似乎成功地将每个 zip 文件条目作为字节数组检索,但这就是您所说的需要帮助。
-
我的 zip 文件包含 2 个文件,每个文件都应转换为 byte[] 并作为 2 个单独的字节数组返回给其他应用程序
-
这是解压zip文件的好答案我希望你能得到解决方案Click Here