Java 解压文件夹时遇到问题-从一个文件夹到另一个文件夹的文件
下面是我将文件夹解压缩到目标文件夹的方法和方法定义Java 解压文件夹时遇到问题-从一个文件夹到另一个文件夹的文件,java,android,unzip,Java,Android,Unzip,下面是我将文件夹解压缩到目标文件夹的方法和方法定义 unzip(filepath, unzipLocation); 这是我解压一个zip文件的方法。这个方法可以解压一个zip文件,但是当我的zip文件像一个.zip文件,它有文件夹2文件夹(即abc1,abc2)和文件夹abc1有文件夹,它有文件时会出问题。请帮助我 private void unzip(String src, String dest) { String _location = ""; final
unzip(filepath, unzipLocation);
这是我解压一个zip文件的方法。这个方法可以解压一个zip文件,但是当我的zip文件像一个.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();
}
}
private void解压(String src,String dest){
字符串_location=“”;
最终整数缓冲区大小=4096;
_地点=目的地;
System.out.println(“\u位置::”+dest+”);
System.out.println(“src::“+src+”);
BufferedOutputStream BufferedOutputStream=null;
FileInputStream FileInputStream;
试一试{
fileInputStream=新的fileInputStream(src);
ZipInputStream ZipInputStream=新的ZipInputStream(新的BufferedInputStream(fileInputStream));
齐彭特里;
而((zipEntry=zipInputStream.getnextery())!=null){
字符串zipEntryName=zipEntry.getName();
字符串名称=dest.substring(dest.lastIndexOf(“/”)-1);
//System.out.println(“名称”+名称);
//文件名=新文件(FolderName);
文件名=新文件(_location.toString());
如果(!FileName.isDirectory()){
试一试{
if(FileName.mkdir()){
}否则{
}
}捕获(例外e){
e、 printStackTrace();
}
}
字符串loc=”“;
字符串fname=“”;
字符串LOC=”“;
LOC=_location.toString();
//文件文件=新文件(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(“检查::“+文件[0]+”);
String test=zipEntryName.substring(zipEntryName.lastIndexOf(“/”),zipEntryName.length());
System.out.println(“TEST”+TEST+”);
如果(test.length()>1){
LOC=“”;
//zipEntryName=文件[1];
zipEntryName=file[file.length-1];
System.out.println(“ZIP更新”+zipEntryName+”);
//_location=_location+“/”+文件[0]+“/”;
字符串l=“”;
for(int i=0;i
公共静态void unZipIt(字符串zipFile,字符串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();
}
}