早上看到一篇关于压缩文件的处理文章,测试之后发现解压缩unzip没有问题,但是对于压缩文件,不支持嵌套,对于空文件夹压缩也会出现错误。
修改了一下,作为加强版,记录如下:
第一步:工具类,组织需要压缩的文件夹下的目录以及文件。
@SuppressWarnings("unchecked")
final static public Vector parse(File[] files, Vector v) {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
v.add(files[i]);
} else {
if (null != files[i].list() && files[i].list().length == 0) {
v.add(files[i]);
} else {
parse(files[i].listFiles(), v);
}
}
}
return v;
}
第二步:处理解析出来的文件路径,变化为相对路径。
final static public String path(String base, String total) {
return total.substring(base.length() - 3, total.length());
}
第三步:压缩处理,注意压缩文件与压缩文件夹的区别。
@SuppressWarnings("unchecked")
final static public void zip(String srcfile, String destfile) {
try {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(destfile)));
byte data[] = new byte[BUFFER];
File f = new File(srcfile);
Vector v = new Vector();
v = parse(f.listFiles(), v);
File file = null;
String path = null;
for (int i = 0; i < v.size(); i++) {
file = (File) v.elementAt(i);
path = path(srcfile, file.getAbsolutePath());
if (file.isFile()) {
origin = new BufferedInputStream(new FileInputStream(file),
BUFFER);
ZipEntry entry = new ZipEntry(path);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
} else {
ZipEntry entry = new ZipEntry(path + "/");
out.putNextEntry(entry);
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
OK,测试代码
@SuppressWarnings("unchecked")
public static void main(String[] args) {
zip("D://zipunzip//testzip//", "D://zipunzip//dest.zip");
unzip("D://zipunzip//dest.zip", "D://zipunzip//testunzip//");
}
另外:附加原始的解压缩代码:
@SuppressWarnings("unchecked")
final static public void unzip(String srcfile, String destfile) {
try {
ZipFile zipFile = new ZipFile(srcfile);
Enumeration emu = zipFile.entries();
while (emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) emu.nextElement();
if (entry.isDirectory()) {
new File(destfile + entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile
.getInputStream(entry));
File file = new File(destfile + entry.getName());
File parent = file.getParentFile();
if (parent != null && (!parent.exists())) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
fos.close();
bos.close();
bis.close();
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
现在想研究一下直接使用字节流来解析压缩文件,看看相关压缩文件的格式,应该不成问题。
接下来写好了发布。
分享到:
相关推荐
源码包含了封装的功能模块类以及测试类, 全部源码都可以用VS2020编译通过,请放心使用! 友情提示,为了兼顾一些旧代码,预编译头文件用的是StdAfx.h,如果需要更换为最新的pch.h,请按照以下步骤操作: 1将StdAfx....
我们要解压缩tar文件,解压缩zip文件,然后在zip中处理文件。 示例0 此示例显示在.tgz文件之前,一切按预期进行。 0. cd example-0 1. gw clean unzipAndRezipZip 2. tree . ├── build │ └── zipOutput ...
* 描述:压缩/解压缩zip包处理类 * 创建者:XXX * 创建日期:2015年5月7日 - 下午1:35:02 * 版本: V0.1 * 修改者: * 修改日期: */ public class ZipUtil { /** * * ...
Unzip提供类似于简单API,用于解析和提取zip文件。 没有添加已编译的依赖项-通货膨胀由node.js内置的zlib支持处理。 解压缩也是的示例用例。 安装 $ npm install unzip 快速范例 提取到目录 fs . createReadStream...
且能对此类类档案进行处理,包括产生压缩档、列出压缩档的内容以及解压缩档案等等。由于能够在伺服器端进行压缩与解压缩的动作,所以相当方便使用。 PclZip定义一个PclZip类别,其类别物件可视为一个ZIP档案,亦...
文中介绍了Linux下处理.zip文件的zip和unzip程序,讲解了压缩文件的zip命令及其参数。
GDAL/OGR Licensing ... Info-ZIP retains the right to use the names "Info-ZIP," "Zip," "UnZip," "UnZipSFX," "WiZ," "Pocket UnZip," "Pocket Zip," and "MacZip" for its own source and binary releases.
当解压合并结束后,程序将PROVINCE_DIR路径下的文件移动到DONE_FILE_PATH下,并且删除PROVINCE_DIR跟UN_ZIP_PATH下文件,这样保证程序每次运行PROVINCE_DIR文件夹下的文件跟UN_ZIP_PATH下的文件都是最新未处理过的,...
4.0(大师) ^ 7.4 | ^ 8.0 当前的^ 3.0 ^ 5.5 | ^ 7.0 目录文献资料\PhpZip\ZipFile类的方法概述创建/打开ZIP归档文件从档案中读取条目迭代条目获取有关条目的信息将条目添加到存档从档案中删除条目处理条目和存档...
soundscape_quality 声音处理质量估计的数据处理和基线方法请在参考论文下载数据集: wget http://users.iit.demokritos.gr/~tyianak/soundscape_quality_dataset/features.zipunzip features.zip -d .wget ...
PeaZip是适用于Windows和Linux的免费文件存档工具和rar提取器,可处理200多种存档类型和变体(7z,ace,arc,bz2,cab,gz,iso,paq,pea,rar,tar,tar,wim,zip, zipx ...),处理跨区存档(001,r01,z01 .....
// 分别处理单个文件/目录的entry if(rootStr.equals(tempFile.getPath())){ entryStr = tempFile.getName(); }else{ entryStr = tempFile.getPath().substring((rootStr+File.separator)....
实际应用中有时候会遇到需要处理 ZIP 压缩解压的情况,这时候我们有大概三种选择: 调用 rar.exe, unzip.exe 等 使用某现成库 完全手写 第一种虽然能完成任务,但是没法知晓结果。曾经有人对说,可以抓命令行输出...
除了标准的ZIP 档案,unzip 功能更支援ZIP 及ZIPX 格式,附加BZIP2, LZMA 及PPMd 压缩技术者。 Unrar 指令能处理所有RAR 版本的压缩档,包含最新的RAR5。 另外UDF 及ISO9660 ISO 档案格式都能解压缩。
linux下提供了zip和unzip程式,zip是压缩程式,unzip是解压程式。他们的参数选项非常多,这里只做简单介绍,依旧举例说明一下其用法: # zip all.zip *.jpg 这条命令是将所有.jpg的文件压缩成一个zip包 # ...
$ wget https://github.com/Ralt/erinome-native/releases/download/0.0.2/erinome-native.zip$ unzip erinome-native.zip$ cd erinome-native/$ sudo make install-linux-chromium发布前的路线图 重构以创建和使用...
1.下载地址: ...wget ...yum install -y unzip zip 在/usr/local/下新建目录rocketmq mkdir rocketmq 然后将解压目录剪切到rocket目录
介绍带有Mongo的Koa2 /宁静的api /测试基本中间件:错误处理程序/授权只需写你的控制者用法# download boilerplate and unzip it to your project namewget -O Holmes.zip ...unzip Holmes.zip && mv Holmes-master {...
unzip matlab_R2020a_glnxa64.zip -d matlab cd matlab sudo ./install 再次使用您的WPI帐户登录以获得许可。 下产品添加 图像采集工具箱 图像处理工具箱 在选项下,选中复选框 建立与MATLAB脚本的链接 安装MATLAB ...
unzip matlab_R2020a_glnxa64.zip -d matlab cd matlab sudo ./install 再次使用您的WPI帐户登录以获得许可。 下产品添加 图像采集工具箱 图像处理工具箱 在选项下,选中复选框 建立与MATLAB脚本的链接 安装MATLAB ...