今天主要介绍JAVA处理ZIP文件,JAVA提供了相应的类、方法来处理ZIP的压缩文件:
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")));
zfile.delete();
}
}
}
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;
}
此解压方法支持压缩文件里面嵌套压缩文件(zip格式的压缩文件)
递归获得文件夹下面所有的文件:
public static List<Map> getAllFile(String path)
{
if(path.indexOf(".zip")>0)
{
zipToFile(path,tempPath);
getAllFile(tempPath);
}else{
File dir = new File(path);
File[] files = dir.listFiles();
if (files == null)
{
return null;
}
for (int i = 0; i < files.length; i++) {
Map map = new HashMap();
if (files[i].isDirectory()) {
getAllFile(files[i].getAbsolutePath());
} else {
String filename = files[i].getName();
if(filename.indexOf(".zip")>0)
{
zipToFile(files[i].getAbsolutePath(),tempPath+"\\tempfile_"+i);
getAllFile(tempPath+"\\tempfile_"+i);
}else{
map.put("filepath", files[i].getAbsolutePath());
map.put("filename", files[i].getName());
filelist.add(map);
}
}
}
}
return filelist;
}
分享到:
相关推荐
这通常包括解压缩文件,然后将补丁文件复制到服务器的相应目录,或者通过控制台或命令行工具执行升级操作。在应用补丁后,建议进行系统测试,确认问题已得到解决,并且其他功能没有受到影响。 总的来说,这个补丁的...
标题中的“24点程序(没有重复,括号显示完美)”指的是一个使用Java编程语言实现的游戏程序,它能够解决经典的数学游戏——24点。在这个游戏中,玩家需要使用加、减、乘、除四种基本运算,以及括号来重新排列四个...
- 解压缩文件即可使用。 ##### 1.3 安装 Tomcat - **原因**:Tomcat 是一个开源的 Servlet 容器,用于部署和运行基于 Java 的 Web 应用程序。 - **步骤**: - 访问 [http://tomcat.apache.org/]...
- 解压缩文件并按照说明进行安装。 - **已知的UNIX编译问题**: - 描述了一些在不同UNIX系统下可能出现的编译错误或警告,并提供了相应的解决办法。 - **编译源代码(Windows系统)**: - 在Windows环境下,...
Horton拥有丰富的教学经验(教学内容包括C、C++、Fortran、PL/1、APL等),同时还是机械、加工和电子CAD系统、机械CAM系统和DNC/CNC系统方面的专家。Ivor Horton还著有Beginning Visual C++ 6、Beginning C ...
2.8 子查询解嵌套 39 2.9 谓语前推 42 2.10 使用物化视图进行查询重写 44 2.11 确定执行计划 46 2.12 执行计划并取得数据行 50 2.13 SQL执行——总览 52 2.14 小结 53 第3章 访问和联结方法 55 3.1 全扫描...