您好,登錄后才能下訂單哦!
class ZipFileHandle{
private String[] FileNameArray; //真實文件名存放數(shù)組
private String[] FileNameArrayShow; //需要顯示的文件名存放數(shù)組
private ZipInputStream zipFile; //zip輸入流對象
private ZipEntry entry; //zip文件入口對象
private int zipFileCount = 0; //zip中的文件總數(shù)
/**
*初始化各個參數(shù)
*通過類的套嵌來訪問文件
*將得到文件的清單附值給數(shù)組,以便在后面用戶選擇時從數(shù)組中獲得文件名
*/
public ZipFileHandle(File file){
try{
while (!(file.exists())) {
System.out.println("Plase input right path again: ");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String filepath = userInput.readLine();
file = new File(filepath);
}
zipFile = new ZipInputStream(new FileInputStream(file));
while ((zipFile.getNextEntry()) != null){
zipFileCount++;
}
FileNameArray = new String[zipFileCount];
FileNameArrayShow = new String[zipFileCount];
}catch(IOException e){
System.out.println("初始化錯誤?。?!");
e.printStackTrace();
}
}
/**
*生成文件目錄
*根據(jù)show的值來確定返回值
*如果show的值為"sh"則返回在屏幕上顯示的名稱
*如果show的值為"gr"則返回實際名稱
*/
public String[] getFileNameList(String show,File file){
try{
int i = 0;
String FileName;
zipFile = new ZipInputStream(new FileInputStream(file));
while ((entry = zipFile.getNextEntry()) != null){
FileName = entry.getName();
//真實文件名附值
FileNameArray[i] = FileName;
//顯示文件名附值
if (FileName.equals("")) FileName = "...";
FileName = Integer.toString(i) + "-|" + FileName;
FileNameArrayShow[i] = FileName;
i++;
}
zipFile.close();
//根據(jù)條件返回文件數(shù)組
if(show == "sh"){
return FileNameArrayShow;
}else{
return FileNameArray;
}
}catch(IOException e){
System.out.println("讀取zip文件內(nèi)的文件名出錯!?。?);
e.printStackTrace();
return null;
}
}
/**
*讀取文件內(nèi)容
*根據(jù)傳遞進來的zip文件對象和
*zip中所要顯示的文件
*用戶根據(jù)type來選擇返回的是unicode信息還字節(jié)信息
*如果type是"Str"則返回字符串信息,"Byte"則返回通過字節(jié)獲得的文件內(nèi)容
*/
public String loadFileCon(String fileName,File file,String type){
String fileContent = "",conTemp = "";
byte[] fileConfByte;
try{
//找到要顯示的文件入口,然后讀取通過文本格式讀取文件內(nèi)容
zipFile = new ZipInputStream(new FileInputStream(file));
//讀取文件內(nèi)容
while ((entry = zipFile.getNextEntry()) != null){
if (entry.getName().equals(fileName)){
//通過字節(jié)讀取文件內(nèi)容
if (type.equals("Byte")){
fileConfByte = new byte[(int)entry.getSize()];
DataInputStream reader = new DataInputStream(zipFile);
reader.readFully(fileConfByte,0,(int)entry.getSize());
fileContent = new String(fileConfByte);
}else if ((type.equals("Str"))){
//通過unicode字符讀取文件內(nèi)容
BufferedReader in = new BufferedReader(new InputStreamReader(zipFile));
while ((conTemp = in.readLine()) != null){
fileContent = fileContent + conTemp + "
";
}
}
}
}
//關閉文件zip流
zipFile.closeEntry();
zipFile.close();
//返回
return fileContent;
}catch(IOException e){
System.out.println("讀取文件內(nèi)容失敗?。?!");
e.printStackTrace();
return null;
}
}
/**
*演示方法
*/
public static void main(String args[]){
String filePath;
String[] showFileArr;
String[] realFileArr;
String fileContent;
int choose;
//通過用戶獲得文件路徑和文件名
try{
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Plase zip file path and name !?。?);
filePath = userInput.readLine();
File file = new File(filePath);
while (!(file.exists())) {
System.out.println("Plase input right path again: ");
filePath = userInput.readLine();
file = new File(filePath);
}
//顯示文件目錄,顯示選擇的文件的內(nèi)容
//獲得真實文件名和顯示文件名數(shù)組
ZipFileHandle zipFile = new ZipFileHandle(file);
showFileArr = zipFile.getFileNameList("sh",file);
realFileArr = zipFile.getFileNameList("gr",file);
//打印顯示文件名數(shù)組
int i = 0;
while (i < showFileArr.length){
System.out.println(showFileArr[i]);
i++;
}
//通過用戶輸入獲得需要顯示的文件
System.out.println("Plase choose file num?。?!");
choose = Integer.parseInt(userInput.readLine());
if ((choose <0)||(choose > showFileArr.length)){
System.out.println("Plase choose file num retry!??!");
choose = Integer.parseInt(userInput.readLine());
}
//獲得用戶選擇的文件的內(nèi)容
fileContent = zipFile.loadFileCon(realFileArr[choose],file,"Byte");
System.out.println(fileContent);
}catch(Exception e){
System.out.println("測試程序出錯?。?!");
e.printStackTrace();
}
}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。