开发项目过程中,会用到很多工具类,这些就不需要自己花时间去实现了,可以到网上查符合自己要求类,节约时间,提高开发效率。
压缩工具类代码
public class ZipToFile {
public static final String ZIP_FILENAME = “C:XJPDA.zip”;// 需要解压缩的文件名
public static final String ZIP_DIR = “D:wjJava图形用户界面的设计与实现相关源代码”;// 需要压缩的文件夹
public static final String UN_ZIP_DIR = “C:”;// 要解压的文件目录
public static final int BUFFER = 1024;// 缓存大小
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:zipJava图形用户界面的设计与实现相关源代码.zip”);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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;
}
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;
}
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();
}
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;
}
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”;
}
}
以上,是我实际项目中用过的zip文件压缩工具类,仅供参考,有什么好的方法,可以评论区交流。
查看更多关于【专业技能】的文章