在android中解压包含:/sdcard/folder/subfolder\file.xml等文件的文件夹时获取反斜杠
我的压缩文件夹包含带有文件的子文件夹,但在解压缩它时,我无法实现相同的层次结构。我得到的解压结构如下:- /存储/模拟/0/解压缩的\u文件夹/sub\u文件夹\main.png /存储/模拟/0/解压缩的\u文件夹/子\u文件夹\test.xml 因此,在提取它的时候,我无法将sub_文件夹作为目录。 我在提取zip文件时使用了以下代码在android中解压包含:/sdcard/folder/subfolder\file.xml等文件的文件夹时获取反斜杠,android,directory,zip,extract,unzip,Android,Directory,Zip,Extract,Unzip,我的压缩文件夹包含带有文件的子文件夹,但在解压缩它时,我无法实现相同的层次结构。我得到的解压结构如下:- /存储/模拟/0/解压缩的\u文件夹/sub\u文件夹\main.png /存储/模拟/0/解压缩的\u文件夹/子\u文件夹\test.xml 因此,在提取它的时候,我无法将sub_文件夹作为目录。 我在提取zip文件时使用了以下代码 public static void unzip(String zipFile, String location) throws IOException {
public static void unzip(String zipFile, String location) throws IOException {
try {
File f = new File(location);
if (!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = location + File.separator + ze.getName();
if (ze.isDirectory()) {
File unzipFile = new File(path);
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
FileOutputStream fout = new FileOutputStream(path, false);
try {
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
} finally {
fout.close();
}
}
}
} finally {
zin.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ZIP STU", "Unzip exception", e);
}
}
请帮帮我,我已经被困在这两天多了。
谢谢 最后,我能够使用下面的代码解决这个问题
public static void unzipEPub(File zipFile, File destinationDir){
ZipFile zip = null;
try {
int DEFUALT_BUFFER = 1024;
destinationDir.mkdirs();
zip = new ZipFile(zipFile);
Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
while (zipFileEntries.hasMoreElements()) {
ZipEntry entry = zipFileEntries.nextElement();
String entryName = entry.getName();
entryName = entryName.replace("\\","/");
File destFile = new File(destinationDir, entryName);
File destinationParent = destFile.getParentFile();
if (destinationParent != null && !destinationParent.exists()) {
destinationParent.mkdirs();
}
if (!entry.isDirectory()) {
BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
int currentByte;
byte data[] = new byte[DEFUALT_BUFFER];
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER);
while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) > 0) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException ignored) {
}
}
}
}
public static void unzipEPub(文件zipFile,文件destinationDir){
ZipFile zip=null;
试一试{
int DEFUALT_BUFFER=1024;
destinationDir.mkdirs();
zip=新ZipFile(ZipFile);
枚举请将代码设置为缩进格式。感谢您的回复。您现在可以查看。