資源簡介
IO.rar

代碼片段和文件信息
package?fileJar;
import?java.io.BufferedInputStream;
import?java.io.BufferedOutputStream;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.OutputStream;
import?java.util.ArrayList;
import?java.util.Enumeration;
import?java.util.List;
import?java.util.jar.JarEntry;
import?java.util.jar.JarFile;
import?java.util.jar.JarOutputStream;
import?java.util.zip.ZipEntry;
public?class?FileJar?{
/**
?*?TODO(壓縮jar包)
?*?
?*?@param?folderUrl
?*????????????被壓縮文件夾路徑
?*?@param?jarUrl
?*????????????生成jar?的文件路徑
?*?@throws?FileNotFoundException
?*?@throws?IOException?void
?*?@author?Administrator
?*/
public??void?createJar(String?folderUrl?String?jarUrl)?throws?FileNotFoundException
IOException?{
//
List?ret?=?getFilesList(new?File(folderUrl));
JarOutputStream?jots?=?new?JarOutputStream(new?FileOutputStream(jarUrl));
JarEntry?je?=?new?JarEntry(““);
byte[]?buf?=?new?byte[1024];???
int?readLen?=?0;
for?(int?i?=?0;?i? File?file?=?(File)?ret.get(i);
je?=?new?JarEntry(getAbsFileName(folderUrl?file));
je.setSize(file.length());
je.setTime(file.lastModified());
jots.putNextEntry(je);
InputStream?is?=?new?BufferedInputStream(new?FileInputStream(file));
while?((readLen?=?is.read(buf?0?1024))?!=?-1)?{??
jots.write(buf?0?readLen);??
}
is.close();
}
jots.close();
}
/**
?*?TODO??解壓jar包
?*?
?*?@param?folderUrl
?*????????????解壓后放入的文件夾
?*?@param?jarUrl
?*????????????解壓的jar包
?*?@throws?FileNotFoundException
?*?@throws?IOException?void
?*?@author?Administrator
?*/
public??void?readJar(String?folderUrl?String?jarUrl)?throws?FileNotFoundException
IOException?{
JarFile?jarFile?=?new?JarFile(jarUrl);
System.out.println(jarFile.getName());
Enumeration?entity?=?jarFile.entries();
ZipEntry?ze?=?null;
byte[]?buf?=?new?byte[1024];
while?(entity.hasMoreElements())?{
ze?=?(ZipEntry)?entity.nextElement();
if?(ze.isDirectory())?{
continue;
}
OutputStream?os?=?new?BufferedOutputStream(new?FileOutputStream(getRealFileName(
folderUrl?ze.getName())));
InputStream?is?=?new?BufferedInputStream(jarFile.getInputStream(ze));
int?readLen?=?0;
while?((readLen?=?is.read(buf?0?1024))?!=?-1)?{
os.write(buf?0?readLen);
}
is.close();
os.close();
}
jarFile.close();
}
/**
?*?TODO??刪除jar?
?*?@param?url
?*?@return?boolean
?*?@author?Administrator?
?*/
public??boolean?removeJar(String?url)?{
boolean?vali?=?false;
File?delFile?=?new?File(url);
if?(delFile.exists()&&delFile.isDirectory())?{
if(delFile.listFiles().length==0){//
delFile.delete();
}
else{
File[]?fileNum?=?delFile.listFiles();
int?num?=?fileNum.length;
for(int?i?=0?;i
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4732??2011-08-17?16:07??IO\FileJar.java
?????文件???????3050??2011-08-11?18:59??IO\Map_RadmonAccessFile.java
?????文件???????2721??2011-08-15?09:19??IO\ReadFile.java
?????目錄??????????0??2011-08-17?16:09??IO
-----------?---------??----------?-----??----
????????????????10503????????????????????4
- 上一篇:Project項目管理 42個案例
- 下一篇:天龍八部腳本易代碼大全
評論
共有 條評論