解压缩文件夹时出现问题 - 文件从一个文件夹到另一个文件夹
getting issue in unzipping folders - files from folder to folder
下面是我将文件夹解压缩到目标文件夹的方法和方法定义
unzip(filepath, unzipLocation);
这是我解压缩 zip 文件的方法此方法有效文件但是当我的 zip 文件出现时出现问题 a.zip 其中有文件夹 2 文件夹(即 abc1,abc2)并且文件夹 abc1 有文件夹和它有文件请帮助我
private void unzip(String src, String dest) {
String _location = "";
final int BUFFER_SIZE = 4096;
_location = dest;
System.out.println("_location ::: " + dest + "");
System.out.println("src ::: " + src + "");
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(src);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String zipEntryName = zipEntry.getName();
String name = dest.substring(dest.lastIndexOf("/") - 1);
// System.out.println("NAME "+name);
// File FileName = new File(FolderName);
File FileName = new File(_location.toString());
if (!FileName.isDirectory()) {
try {
if (FileName.mkdir()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
String loc = "";
String fname = "";
String LOC = "";
LOC = _location.toString();
// File file = new File(FolderName+"/" +zipEntryName);
System.out.println("ZIP " + zipEntryName + "");
// zipEntryName=zipEntryName.r
if (zipEntryName.contains("/")) {
String[] file = zipEntryName.split("/");
System.out.println("CHECK ::: " + file.length);
System.out.println("CHECK ::: " + file[0] + "");
String test = zipEntryName.substring(zipEntryName.lastIndexOf("/"), zipEntryName.length());
System.out.println("TEST " + test + "");
if (test.length() > 1) {
LOC = "";
// zipEntryName = file[1];
zipEntryName = file[file.length - 1];
System.out.println("ZIP UPDATED " + zipEntryName + "");
// _location=_location+"/"+file[0]+"/";
String l = "";
for (int i = 0; i < file.length - 1; i++) {
l = l + "/" + file[i];
}
// System.out.println("TESTTTTTTTT " + l + "");
// loc = _location + "/" + file[0];
// loc = _location + "/" + l;
LOC = _location + "/" + l;
File thumb = new File(LOC);
if (!thumb.exists()) {
thumb.mkdir();
}
System.out.println("createddd dir ");
System.out.println("createddd dir loc " + loc);
} else {
System.out.println("create dir ");
System.out.println("HERE _location111: : : : " + _location);
System.out.println("HERE zipEntryName1111 : : : : /" + zipEntryName);
// File thumb = new File(_location+"/"+zipEntryName);
File thumb = new File(LOC + "/" + zipEntryName);
if (!thumb.exists()) {
thumb.mkdir();
}
}
}
System.out.println("HERE _location: : : : " + _location);
System.out.println("HERE zipEntryName : : : : /" + zipEntryName);
System.out.println("HERE loc : : : : /" + loc);
// File file = new File(_location + "/" + zipEntryName);
File file = new File(LOC + "/" + zipEntryName);
if (file.exists()) {
} else {
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;
while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void unZipIt(String zipFile, String outputFolder) {
byte[] buffer = new byte[1024];
//mm System.out.println("ZIP FILE "+zipFile+"");
//mm System.out.println("outputFolder FILE "+outputFolder+"");
try {
//create output directory is not exists
File folder = new File(outputFolder);
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();
File newFile = new File(outputFolder + File.separator + fileName);
//mm System.out.println("file unzip : " + newFile.getAbsoluteFile());
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
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();
}
}
下面是我将文件夹解压缩到目标文件夹的方法和方法定义
unzip(filepath, unzipLocation);
这是我解压缩 zip 文件的方法此方法有效文件但是当我的 zip 文件出现时出现问题 a.zip 其中有文件夹 2 文件夹(即 abc1,abc2)并且文件夹 abc1 有文件夹和它有文件请帮助我
private void unzip(String src, String dest) {
String _location = "";
final int BUFFER_SIZE = 4096;
_location = dest;
System.out.println("_location ::: " + dest + "");
System.out.println("src ::: " + src + "");
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(src);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String zipEntryName = zipEntry.getName();
String name = dest.substring(dest.lastIndexOf("/") - 1);
// System.out.println("NAME "+name);
// File FileName = new File(FolderName);
File FileName = new File(_location.toString());
if (!FileName.isDirectory()) {
try {
if (FileName.mkdir()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
String loc = "";
String fname = "";
String LOC = "";
LOC = _location.toString();
// File file = new File(FolderName+"/" +zipEntryName);
System.out.println("ZIP " + zipEntryName + "");
// zipEntryName=zipEntryName.r
if (zipEntryName.contains("/")) {
String[] file = zipEntryName.split("/");
System.out.println("CHECK ::: " + file.length);
System.out.println("CHECK ::: " + file[0] + "");
String test = zipEntryName.substring(zipEntryName.lastIndexOf("/"), zipEntryName.length());
System.out.println("TEST " + test + "");
if (test.length() > 1) {
LOC = "";
// zipEntryName = file[1];
zipEntryName = file[file.length - 1];
System.out.println("ZIP UPDATED " + zipEntryName + "");
// _location=_location+"/"+file[0]+"/";
String l = "";
for (int i = 0; i < file.length - 1; i++) {
l = l + "/" + file[i];
}
// System.out.println("TESTTTTTTTT " + l + "");
// loc = _location + "/" + file[0];
// loc = _location + "/" + l;
LOC = _location + "/" + l;
File thumb = new File(LOC);
if (!thumb.exists()) {
thumb.mkdir();
}
System.out.println("createddd dir ");
System.out.println("createddd dir loc " + loc);
} else {
System.out.println("create dir ");
System.out.println("HERE _location111: : : : " + _location);
System.out.println("HERE zipEntryName1111 : : : : /" + zipEntryName);
// File thumb = new File(_location+"/"+zipEntryName);
File thumb = new File(LOC + "/" + zipEntryName);
if (!thumb.exists()) {
thumb.mkdir();
}
}
}
System.out.println("HERE _location: : : : " + _location);
System.out.println("HERE zipEntryName : : : : /" + zipEntryName);
System.out.println("HERE loc : : : : /" + loc);
// File file = new File(_location + "/" + zipEntryName);
File file = new File(LOC + "/" + zipEntryName);
if (file.exists()) {
} else {
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;
while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void unZipIt(String zipFile, String outputFolder) {
byte[] buffer = new byte[1024];
//mm System.out.println("ZIP FILE "+zipFile+"");
//mm System.out.println("outputFolder FILE "+outputFolder+"");
try {
//create output directory is not exists
File folder = new File(outputFolder);
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();
File newFile = new File(outputFolder + File.separator + fileName);
//mm System.out.println("file unzip : " + newFile.getAbsoluteFile());
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
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();
}
}