Two days ago to see the "Java core programming" in Java in the IO input and output to explain, harvested a lot, the following program is I read the book after I wrote a file in the zip file to read out the contents and then display (only text, other files will produce garbled), This program can also be improved to get a class with the same WinZip functionality (but there is no interface, mainly AWT will not:--(), two days after the source code posted. A very simple class, hope to be useful to everyone.
This is my first time to send articles, deficiencies please advise!!!
/**
* Function: Zip file release, zip file directory, select directory, display file contents
* First user input zip file path and name, read the zip file to the file set of file list display,
* The user selects the file to be displayed, then reads the contents of the file through the DataInputStream class and
* Display on standard output
* Date: 2005-06-27
* Author: Pcera
*/
Import java.util.*;
Import java.util.zip.*;
Import java.io.*;
Class zipfilehandle{
Private string[] Filenamearray; Real file name Store array
Private string[] filenamearrayshow; The file name that needs to be displayed stores the array
Private Zipinputstream ZipFile; Zip input Stream Object
Private ZipEntry entry; ZIP File Entry Object
private int zipfilecount = 0; Total number of files in zip
/**
* Initialize each parameter
* Access to files through nested nesting of classes
* Append the list of files to the array to get the file name from the array when the user chooses later
*/
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 ("Initialization error!!! ");
E.printstacktrace ();
}
}
/**
* Generate file directory
* To determine the return value based on the value of show
* If show's value is "sh", return the name displayed on the screen
* If show's value is "GR" then return the actual name
*/
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 ();
Real file name appended value
Filenamearray[i] = FileName;
Display file name appended value
if (Filename.equals ("")) FileName = "...";
filename = integer.tostring (i) + "-|" + filename;
Filenamearrayshow[i] = FileName;
i++;
}
Zipfile.close ();
Returns an array of files based on criteria
if (show = = "sh") {
return filenamearrayshow;
}else{
return filenamearray;
}
}catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Error in reading file name in ZIP file!!! ");
E.printstacktrace ();
return null;
}
}
/**
* Read the contents of the file
* Based on the incoming ZIP file object and
Files to be displayed in the *zip
* The user chooses to return Unicode information or byte information based on type
* If type is "Str" returns the string information, "byte" returns the contents of the file obtained by byte
*/
public string Loadfilecon (string filename,file file,string type) {
String filecontent = "", contemp = "";
Byte[] Fileconfbyte;
try{
Locate the file entry to display and read the contents of the file in text format
ZipFile = new Zipinputstream (new FileInputStream (file));
Read File contents
while ((entry = Zipfile.getnextentry ())!= null) {
if (Entry.getname (). Equals (FileName)) {
Read file contents through bytes
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")) {
Reading file contents through Unicode characters
BufferedReader in = new BufferedReader (new InputStreamReader (ZipFile));
while ((contemp = In.readline ())!= null) {
Filecontent = filecontent + contemp + "\n\r";
}
}
}
}
Close File Zip stream
Zipfile.closeentry ();
Zipfile.close ();
Return
return filecontent;
}catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Read file contents failed!!! ");
E.printstacktrace ();
return null;
}
}
/**
* Demo Method
*/
public static void Main (String args[]) {
String FilePath;
String[] Showfilearr;
String[] Realfilearr;
String filecontent;
int choose;
Get file path and filename from user
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);
}
Displays the file directory, showing the contents of the selected file
Get the real file name and array of display file names
Zipfilehandle ZipFile = new Zipfilehandle (file);
Showfilearr = zipfile.getfilenamelist ("sh", file);
Realfilearr = Zipfile.getfilenamelist ("gr", file);
Print Display file name array
int i = 0;
while (I < showfilearr.length) {
System.out.println (Showfilearr[i]);
i++;
}
Get the files you want to display with user input
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 ());
}
Get the contents of the file selected by the user
Filecontent = Zipfile.loadfilecon (Realfilearr[choose],file, "Byte");
System.out.println (filecontent);
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("TEST program error!!! ");
E.printstacktrace ();
}
}
}