Java 如何在android中解压包含图像的Zip文件而不删除任何图像?
我正在解压的zip文件,其中包含图像只有在这里我的代码是给定的Java 如何在android中解压包含图像的Zip文件而不删除任何图像?,java,android,zip,unzip,Java,Android,Zip,Unzip,我正在解压的zip文件,其中包含图像只有在这里我的代码是给定的 public void unZipImages(String zipFile, String outputFolder) { byte[] buffer = new byte[2048]; try { // create output directory is not exists File folder = new File(Environment.getExternalStorageDirecto
public void unZipImages(String zipFile, String outputFolder) {
byte[] buffer = new byte[2048];
try {
// create output directory is not exists
File folder = new File(Environment.getExternalStorageDirectory()
+ "/final_unzip_data/" + AppConstants.ManufacturerCode
+ "/Catalog/" + ""); if (!folder.exists()) {
folder.mkdir(); }
// get the zip file content
ZipInputStream zis = new ZipInputStream(
new FileInputStream(zipFile)); // get the zipped file list entry ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
if (fileName.contains("\\")) {
fileName = fileName.replace("\\", "/");
}
File newFile = new File(outputFolder + File.separator
+ fileName);
System.out.println("file unzip : " + newFile.getAbsoluteFile());
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry(); }
zis.closeEntry(); zis.close();
System.out.println("Done");
} catch (IOException ex) { ex.printStackTrace(); } }
但是我不知道解压文件时发生了什么错误,一些图像正在删除。我已经搜索了很多类型的代码来解压,但是没有任何效果。只要看一看,这个库文件既快又好
你有使用图书馆的简短说明
仅供参考:这是另一个参考
关于你的代码,我已经在我的项目中尝试过这个片段,但是解压时忽略了小图像,所以我使用了上面的库。现在我可以把它重新解决了 此代码解决了我的问题
private void unzipImage(String zipFile, String extractFolder) {
try {
CreateDir();
int BUFFER = 4096;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = extractFolder;
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
// Process each entry
while (zipFileEntries.hasMoreElements()) {
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
currentEntry = currentEntry.replace('\\', '/');
File destFile = new File(newPath, currentEntry);
// destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory()) {
BufferedInputStream is = new BufferedInputStream(
zip.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
zip.close();
}
} catch (Exception e) {
Log.e("ERROR: ", "" + e.getMessage());
}
}
等等,我必须检查methods.java.io.FileNotFoundException:/mnt/sdcard/final_unzip_data/Nor/images\2014 Spring Esschert.JPG:open失败:EINVAL(无效参数)Bro,我搜索了很多,找到了更多链接,然后创建了我的代码,所以我在你面前分享了。是的!!我理解,这是没有担心,非常感谢,因为我们都可以使用这个解决方案+1的问题和+1的答案从我这边!!谢谢兄弟,我也读了你的博客。谢谢!不要忘记关闭zip文件:
zip.close()代码>@Luciano是的,我添加了它。