iissky1 2008-10-21 09:49
浏览 223
已采纳

一个java解压文件的递归调用 急

下面是接压单个压缩文件的代码,那请问,如果我压缩文件里面还有压缩文件怎么递归调用全部接压呢?接压后应按照接压前的包路径一致。须急用,请指教
[code="java"]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipToFile {
/**
* 将文件解压缩
* @param sZipPathFile 需要解压的文件
* @param sDestPath 解压后的文件路径
* @return
*/
public static boolean zipToFile(String sZipPathFile, String sDestPath) {
boolean flag = false;
ArrayList allFileName = new ArrayList();
try {
// 先指定压缩档的位置和档名,建立FileInputStream对象
FileInputStream fins = new FileInputStream(sZipPathFile);
// 将fins传入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
allFileName.add(zfile.getAbsolutePath());
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i);
zins.closeEntry();
fouts.close();
}
}
fins.close();
zins.close();
flag = true;
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage());
}
return flag;
}

public static void main(String[] args){
    String sZipPathFile = "c:/test.zip";
    String sDestPath = "c:/";
    ZipToFile.zipToFile(sZipPathFile, sDestPath);//将c盘下的test.zip文件解压到c盘下
}

}

[/code]

  • 写回答

1条回答 默认 最新

  • sptzone 2008-10-21 14:14
    关注

    [code="java"]package test;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;

    public class ZipToFile {

    public static boolean zipToFile(String sZipPathFile, String sDestPath) {
        boolean flag = false;
        try {
    
            FileInputStream fins = new FileInputStream(sZipPathFile);
    
            ZipInputStream zins = new ZipInputStream(fins);
            ZipEntry ze = null;
            byte ch[] = new byte[256];
            while ((ze = zins.getNextEntry()) != null) {
                File zfile = new File(sDestPath + "//" + ze.getName());
                File fpath = new File(zfile.getParentFile().getPath());
                if (ze.isDirectory()) {
                    if (!zfile.exists())
                        zfile.mkdirs();
                    zins.closeEntry();
                } else {
                    if (!fpath.exists())
                        fpath.mkdirs();
                    FileOutputStream fouts = new FileOutputStream(zfile);
                    int i;
                    String zfilePath = zfile.getAbsolutePath();
                    while ((i = zins.read(ch)) != -1)
                        fouts.write(ch, 0, i);
                    zins.closeEntry();
                    fouts.close();
    
                    if (zfilePath.endsWith(".zip")) {
                        zipToFile(zfilePath, zfilePath.substring(0, zfilePath
                                .lastIndexOf(".zip")));
                    }
    
                }
            }
            fins.close();
            zins.close();
    
            // if necessary, delete original zip-file
            File file = new File(sZipPathFile);
    
            file.delete();
            flag = true;
        } catch (Exception e) {
            System.err.println("Extract error:" + e.getMessage());
        }
        return flag;
    }
    
    public static void main(String[] args) {
        String sZipPathFile = "C:/Music.zip";
        String sDestPath = "C:/";
        ZipToFile.zipToFile(sZipPathFile, sDestPath);
    }
    

    }[/code]

    1.[code="java"]File zfile = new File(sDestPath + ze.getName()); [/code]
    应改为:
    [code="java"]File zfile = new File(sDestPath + "//" + ze.getName());[/code]

    2.如果解压出来的不是目录,而是文件,则
    [code="java"]if (zfilePath.endsWith(".zip")) {
    zipToFile(zfilePath, zfilePath.substring(0, zfilePath
    .lastIndexOf(".zip")));
    }[/code]

    3.如果有需要,在fins.close()和zins.close()后,删除zip文件:
    [code="java"]// if necessary, delete original zip-file
    File file = new File(sZipPathFile);

            file.delete();[/code]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波