欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java解压zip文件为空(java解压各种类型的文件)

程序员文章站 2023-11-16 23:02:58
开发项目过程中,会用到很多工具类,这些就不需要自己花时间去实现了,可以到网上查符合自己要求类,节约时间,提高开发效率。压缩工具类代码public class ziptofile {public sta...

开发项目过程中,会用到很多工具类,这些就不需要自己花时间去实现了,可以到网上查符合自己要求类,节约时间,提高开发效率。

java解压zip文件为空(java解压各种类型的文件)

压缩工具类代码

public class ziptofile {

public static final string zip_filename = “c:\xjpda.zip”;// 需要解压缩的文件名

public static final string zip_dir = “d:\wj\java图形用户界面的设计与实现相关源代码\”;// 需要压缩的文件夹

public static final string un_zip_dir = “c:\”;// 要解压的文件目录

public static final int buffer = 1024;// 缓存大小

/**

* zip压缩功能. 压缩basedir(文件夹目录)下所有文件,包括子目录

*

* @throws exception

*/

public static void zipfile(string basedir, string filename)

throws exception {

list filelist = getsubfiles(new file(basedir));

zipoutputstream zos = new zipoutputstream(

new fileoutputstream(filename));

zos.setencoding(system.getproperty(“sun.jnu.encoding”));

zipentry ze = null;

byte[] buf = new byte[buffer];

int readlen = 0;

for (int i = 0; i < filelist.size(); i++) {

file f = (file) filelist.get(i);

ze = new zipentry(getabsfilename(basedir, f));

ze.setsize(f.length());

ze.settime(f.lastmodified());

zos.putnextentry(ze);

inputstream is = new bufferedinputstream(new fileinputstream(f));

while ((readlen = is.read(buf, 0, buffer)) != -1) {

zos.write(buf, 0, readlen);

}

is.close();

}

zos.close();

}

public static void main(string[] args) {

try {

zipfile(zip_dir, “d:\zip\java图形用户界面的设计与实现相关源代码.zip”);

} catch (exception e) {

// todo auto-generated catch block

e.printstacktrace();

}

}

java解压zip文件为空(java解压各种类型的文件)

/**

* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.

*

* @param basedir

* java.lang.string 根目录

* @param realfilename

* java.io.file 实际的文件名

* @return 相对文件名

*/

private static string getabsfilename(string basedir, file realfilename) {

file real = realfilename;

file base = new file(basedir);

string ret = real.getname();

while (true) {

real = real.getparentfile();

if (real == null)

break;

if (real.equals(base))

break;

else

ret = real.getname() + “/” + ret;

}

return ret;

}

/**

* 取得指定目录下的所有文件列表,包括子目录.

*

* @param basedir

* file 指定的目录

* @return 包含java.io.file的list

*/

private static list getsubfiles(file basedir) {

list ret = new arraylist();

file[] tmp = basedir.listfiles();

for (int i = 0; i < tmp.length; i++) {

if (tmp[i].isfile())

ret.add(tmp[i]);

if (tmp[i].isdirectory())

ret.addall(getsubfiles(tmp[i]));

}

return ret;

}

/**

* 解压缩功能. 将zip_filename文件解压到zip_dir目录下.

*

* @throws exception

*/

public static void upzipfile() throws exception {

zipfile zfile = new zipfile(zip_filename);

enumeration zlist = zfile.getentries();

zipentry ze = null;

byte[] buf = new byte[1024];

while (zlist.hasmoreelements()) {

ze = (zipentry) zlist.nextelement();

if (ze.isdirectory()) {

file f = new file(zip_dir + ze.getname());

f.mkdir();

continue;

}

outputstream os = new bufferedoutputstream(new fileoutputstream(

getrealfilename(zip_dir, ze.getname())));

inputstream is = new bufferedinputstream(zfile.getinputstream(ze));

int readlen = 0;

while ((readlen = is.read(buf, 0, 1024)) != -1) {

os.write(buf, 0, readlen);

}

is.close();

os.close();

}

zfile.close();

}

/**

* 给定根目录,返回一个相对路径所对应的实际文件名.

*

* @param basedir

* 指定根目录

* @param absfilename

* 相对路径名,来自于zipentry中的name

* @return java.io.file 实际的文件

*/

public static file getrealfilename(string basedir, string absfilename) {

string[] dirs = absfilename.split(“/”);

file ret = new file(basedir);

if (dirs.length > 1) {

for (int i = 0; i < dirs.length – 1; i++) {

ret = new file(ret, dirs[i]);

}

if (!ret.exists())

ret.mkdirs();

ret = new file(ret, dirs[dirs.length – 1]);

return ret;

}

return ret;

}

/**

* 给zip文件加密方法,需要下载encryptzip.jar包

*

* @param zipdir

* 要加密的压缩文件

* @param encryptzipfile

* 加密后文件

* @param password

*密码

*/

public static void encryptionfile(string zipdir,string encryptzipfile,string password){

system.out.println(“===== 加密 =====”);

file file = new file(zipdir);

byte[] zipbyte = zipoutput.getencryptzipbyte(file.listfiles(), password);

fileutils.writebytefile(zipbyte, new file(encryptzipfile));

system.out.println(“===== encrypt success =====”);

//return “success”;

}

}

java解压zip文件为空(java解压各种类型的文件)

以上,是我实际项目中用过的zip文件压缩工具类,仅供参考,有什么好的方法,可以评论区交流。