简体   繁体   English

Java:解压缩文件,而不创建目标文件夹

[英]Java:Unzipping a file, without creating target folder

I have a nice java code that unzips a .zip file. 我有一个很好的java代码解压缩.zip文件。 But problem with this code is 但是这个代码的问题是

  1. i need to create target folders(Note:Only folders not file) before running this code. 我需要在运行此代码之前创建目标文件夹(注意:只有文件夹不是文件)。
  2. Otherwise i will get path not found exception. 否则我将获得路径未找到异常。

So This code wont work if zip file content is not known before. 因此,如果以前不知道zip文件内容,此代码将无法工作。 so i think this is useless code. 所以我认为这是无用的代码。 Anyone have better logic? 谁有更好的逻辑? or Bellow code need to be edited? 或贝娄代码需要编辑?

package com.mireader;
import android.os.Environment;
import android.util.Log; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

/** 
 * 
 * @author jon 
 */ 
public class Decompress { 
  private String _zipFile; 
  private String _location; 

    public Decompress(String zipFile, String location) { 
        _zipFile = zipFile; 
        _location = location; 

        _dirChecker(""); 
    } 
    public void unzip() { 
        try  { 
            FileInputStream fin = new FileInputStream(_zipFile); 
            ZipInputStream zin = new ZipInputStream(fin); 
            ZipEntry ze = null; 
            byte[] buffer = new byte[1024];
            int length;
            int i=0;  
            while ((ze = zin.getNextEntry()) != null) { 
                Log.v("t", ze.toString());
                Log.v("Decompress", "Unzipping " + ze.getName()); 

                if(ze.isDirectory()) {
                    Log.i("my","Comes to if");
                    _dirChecker(ze.getName()); 
                }
                else { 
                    Log.i("my","Comes to else");
                    FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
                    while ((length = zin.read(buffer))>0) {
                        fout.write(buffer, 0, length);
                    }
                    zin.closeEntry(); 
                    fout.close(); 
                } 
            }
            zin.close(); 
            Log.i("My tag","Success");
        }catch(Exception e) { 
            Log.e("Decompress", "unzip", e); 
        }     

    } 

    private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 
    if(!f.isDirectory()) { 
        Log.i("mytag", "Creating new folder");
        f.mkdirs(); 
        System.out.print("stp:"+f.getName());
    } 
  } 
} 

You can avoid the following piece of code 您可以避免以下代码

if(ze.isDirectory()) {
  Log.i("my","Comes to if");
  _dirChecker(ze.getName()); 
}

and add code similar to the one below in the file creator else part. 并在文件创建者其他部分添加类似于下面的代码。 It worked for me by creating the entire parent folders. 它通过创建整个父文件夹为我工作。

File file = createFile((baseDirectory +"/" + zipFile.getName()));
file.getParentFile().mkdirs();

It doesn't look that wrong, what exactly is your problem? 它看起来不错,究竟是什么问题? You create the directories as they appear in the zip file; 您创建zip文件中显示的目录; I usually do it inline, but it's the same. 我通常是内联的,但它是一样的。 Your code would fail if your zip file not has the directory stubs inside (which could happen with zip creators in the wild), so my unzip code doublechecks the existance of the directory before extracting each file: 如果你的zip文件里面没有目录存根,你的代码会失败(这可能发生在野外的zip创建者),所以我的解压缩代码在提取每个文件之前双重检查目录的存在:

if (zEntry.isDirectory()) new File(destDir+zEntry.getName()).mkdirs();
else 
{
String dstEntryDir=new File(destDir+zEntry.getName()).getParent()+File.separator;
if (!fileExists(dstEntryDir)) new File(dstEntryDir).mkdirs();
copyStreamToFile(zFile.getInputStream(zEntry),destDir+zEntry.getName());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM