Оглавление |
Назад
// ==========================================
// ZipFileView.java
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.text.*;
public class ZipFileView
{
public static void main(String args[])
{
FrameWindow frame;
frame = new FrameWindow(
"View ZIP-files");
frame.setVisible(true);
}
}
// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
implements ActionListener, WindowListener
{
TextArea ta;
MenuBar mb;
Menu mFile;
MenuItem miOpen;
MenuItem miExit;
FileDialog fdlg;
// ============================================
// FrameWindow
// ============================================
public FrameWindow(String szTitle)
{
super(szTitle);
setSize(400, 300);
mb = new MenuBar();
mFile = new Menu("File");
miOpen = new MenuItem("Open...");
mFile.add(miOpen);
mFile.add("-");
miExit = new MenuItem("Exit");
mFile.add(miExit);
mb.add(mFile);
miOpen.addActionListener(this);
miExit.addActionListener(this);
setMenuBar(mb);
this.addWindowListener(this);
ta = new TextArea(10, 30);
setLayout(new BorderLayout());
add("Center", ta);
}
// ============================================
// actionPerformed
// ============================================
public void actionPerformed(ActionEvent e)
{
String szSelected = null;
if(e.getSource().equals(miOpen))
{
fdlg = new FileDialog(this, "Open ZIP-file",
FileDialog.LOAD);
fdlg.setFile("*.zip");
fdlg.show();
if(fdlg.getDirectory() == null ||
fdlg.getFile() == null)
return;
String szPath = fdlg.getDirectory() +
fdlg.getFile();
ZipFileDialog d = new ZipFileDialog(
"ZIP File: ", szPath, this);
d.show();
}
else if(e.getSource().equals(miExit))
{
setVisible(false);
System.exit(0);
}
}
// ============================================
// windowClosing
// ============================================
public void windowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
// ============================================
// Class ZipFileDialog
// ============================================
class ZipFileDialog extends Dialog
implements ActionListener, WindowListener,
ItemListener
{
Frame par;
List l;
Button bExtract;
Button bCancel;
String szBoxTitle;
String szZipFilePath;
ZipFile zf;
Vector zipEntries = new Vector();
String szItemInfo = null;
// ============================================
// ZipFileDialog
// ============================================
public ZipFileDialog(String szTitle,
String szPath, Frame parent)
{
super(parent, szTitle, false);
if(szTitle == null)
return;
par = parent;
setSize(400, 300);
szBoxTitle = szTitle;
szZipFilePath = szPath;
this.addWindowListener(this);
l = new List(5, false);
l.addItemListener(this);
bCancel = new Button("Cancel");
bExtract = new Button("Extract");
bCancel.addActionListener(this);
bExtract.addActionListener(this);
l.addActionListener(this);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c =
new GridBagConstraints();
setLayout(gbl);
// -----------------------
// ListBox
// -----------------------
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0;
c.weighty = 1.0;
gbl.setConstraints(l, c);
add(l);
// -----------------------
// Extract
// -----------------------
c.weightx = 0;
c.weighty = 0;
c.fill = GridBagConstraints.NONE;
c.gridwidth = 1;
c.ipadx = 10;
gbl.setConstraints(bExtract, c);
add(bExtract);
// -----------------------
// Cancel
// -----------------------
c.gridwidth = GridBagConstraints.REMAINDER;
c.ipadx = 10;
c.weightx = 1.0;
gbl.setConstraints(bCancel, c);
add(bCancel);
updateList();
}
// ============================================
// updateList
// ============================================
void updateList()
{
l.removeAll();
try
{
zf = new ZipFile(szZipFilePath);
Enumeration en = zf.entries();
setTitle("ZIP - " + szZipFilePath);
int i = 0;
while(en.hasMoreElements())
{
zipEntries.addElement(
(ZipEntry)en.nextElement());
}
for (i = 0; i < zipEntries.size(); i++)
{
ZipEntry ze =
(ZipEntry)zipEntries.elementAt(i);
l.add(ze.getName());
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
// ============================================
// actionPerformed
// ============================================
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(bExtract))
{
String s = l.getSelectedItem();
if(s != null)
saveZipFile(s);
}
else if(e.getSource().equals(bCancel))
{
setVisible(false);
}
else if(e.getSource().equals(l))
{
String s = l.getSelectedItem();
}
}
// ============================================
// itemStateChanged
// ============================================
public void itemStateChanged(ItemEvent e)
{
getZipFileEntryInfo();
}
// ============================================
// getZipFileEntryInfo
// ============================================
void getZipFileEntryInfo()
{
int nSelected = l.getSelectedIndex();
if(nSelected < 0)
return;
ZipEntry ze =
(ZipEntry)zipEntries.elementAt(nSelected);
((FrameWindow)par).ta.selectAll();
((FrameWindow)par).ta.replaceRange(
"", 0,
((FrameWindow)par).ta.getSelectionEnd());
szItemInfo = "Entry name: " + ze.getName();
szItemInfo += "\nComment: " + ze.getComment();
szItemInfo += "\nCompressed size: " +
ze.getCompressedSize();
szItemInfo += "\nSize: " + ze.getSize();
szItemInfo += "\nCRC-32: " + ze.getCrc();
szItemInfo += "\nExtra data: " + ze.getExtra();
szItemInfo += "\nCRC-32: " + ze.getCrc();
szItemInfo += "\nDate: " +
DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.FULL).format(
new Date(ze.getTime()));
if(ze.getMethod() == ZipEntry.DEFLATED)
szItemInfo += "\nMethod: DEFLATED";
else if(ze.getMethod() == ZipEntry.STORED)
szItemInfo += "\nMethod: STORED";
if(ze.isDirectory())
szItemInfo += "\n- DIRECTORY -";
else
szItemInfo += "\n- FILE -";
((FrameWindow)par).ta.append(szItemInfo);
}
// ============================================
// saveZipFile
// ============================================
void saveZipFile(String s)
{
int nSelected;
byte[] buf = new byte[8000];
FileDialog fdlg;
s = s.substring(s.lastIndexOf("/") + 1);
fdlg = new FileDialog(par, "Save file as...",
FileDialog.SAVE);
fdlg.setFile(s);
fdlg.show();
if(fdlg.getDirectory() == null ||
fdlg.getFile() == null)
return;
String szPath = fdlg.getDirectory() +
fdlg.getFile();
try
{
FileOutputStream fos =
new FileOutputStream(szPath);
nSelected = l.getSelectedIndex();
if(nSelected < 0)
return;
ZipEntry ze =
(ZipEntry)zipEntries.elementAt(nSelected);
InputStream is = zf.getInputStream(ze);
int nLength;
while(true)
{
nLength = is.read(buf);
if(nLength < 0)
break;
fos.write(buf, 0, nLength);
}
is.close();
fos.close();
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
// ============================================
// windowClosing
// ============================================
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
|