Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
test.zip
|
->hallo
->daten
| |
| ->versuch
->test.txt
Math55 hat gesagt.:hallo, jaja, du weisst was ich meine . ich werd die klasse mal testen. nur mit der api ist das irgendwie sehr kompliziert. die eclipse sache sollte ja mit jarsauch gehen, oder?
muss ich dazu swt nutzen? sicher nicht, oder? also ich habe eiclipse als entwicklungsumgebung installiert, aber woher bekomm ich denn das package dafür, wenn ich ohne eclipse arbeite?
gruß und besten DANK
muss ich dazu swt nutzen? sicher nicht, oder?
Thomas Darimont hat gesagt.:Hallo!
"Man sollte nicht immer alles von Hand machen"...
hab meine Lösung eingestampft.
hier die "Musterlösung" -> http://www.java-forum.org/de/viewtopic.php?p=66046#66046
Gruß Tom
/*
* Created on 23.12.2004@20:18:59
*
* TODO Licence info
*
*/
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
/**
* @author Administrator
*
* TODO Explain me
*
*/
public class ZipViewer extends JFrame {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3979269140316238387L;
private JTree tree;
private JButton btn;
private JProgressBar progressBar;
private JFileChooser fileChooser;
private DefaultTreeModel model;
public ZipViewer() {
super("ZipViewer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
tree = new JTree((TreeModel) null);
btn = new JButton("select archive");
progressBar = new JProgressBar();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fileChooser == null) {
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String name = f.getName();
return Pattern.matches(".*.(zip|jar)", name);
}
public String getDescription() {
return "Supported Archiv extensions (zip,jar)";
}
});
}
if (fileChooser.showOpenDialog(ZipViewer.this) == JFileChooser.APPROVE_OPTION) {
final File file = fileChooser.getSelectedFile();
if (file.isFile()) {
System.out.println(file);
new Thread() {
{
setPriority(MIN_PRIORITY);
}
public void run() {
btn.setEnabled(false);
processArchive(file);
btn.setEnabled(true);
}
}.start();
}
}
}
});
Container c = getContentPane();
c.add(new JScrollPane(tree) {
{
setPreferredSize(new Dimension(180, 400));
}
}, BorderLayout.NORTH);
c.add(btn, BorderLayout.CENTER);
c.add(progressBar, BorderLayout.SOUTH);
pack();
setVisible(true);
}
/**
* @param file
*/
protected void processArchive(File file) {
try {
model = new ArchiveTreeModel(new ZipFile(file));
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tree.setModel(model);
}
public static void main(String[] args) {
new ZipViewer();
}
class ArchiveTreeModel extends DefaultTreeModel {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3258689897023093049L;
public ArchiveTreeModel(ZipFile zipFile) {
super(new DefaultMutableTreeNode("/"));
/* */
progressBar.setMinimum(0);
progressBar.setMaximum(zipFile.size());
int value = 0;
/* */
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String name = entry.getName();
String[] elements = name.split("/");
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getRoot();
/* */
progressBar.setValue(++value);
/* */
for (int i = 0; i < elements.length; i++) {
String token = elements[i];
DefaultMutableTreeNode nextNode = findNode(currentNode,
token);
if (nextNode == null) {
nextNode = new DefaultMutableTreeNode(token);
currentNode.add(nextNode);
}
currentNode = nextNode;
}
}
}
private DefaultMutableTreeNode findNode(DefaultMutableTreeNode parent,
String name) {
Enumeration e = parent.children();
while (e.hasMoreElements()) {
DefaultMutableTreeNode element = (DefaultMutableTreeNode) e
.nextElement();
if (element.getUserObject().equals(name)) {
return element;
}
}
return null;
}
}
}
/*
* Created on 23.12.2004@20:18:59
*
* TODO Licence info
*
*/
package de.tutorials.zipviewer;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
/**
* @author Administrator
*
* TODO Explain me
*
*/
public class ArchiveViewer extends JFrame {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3979269140316238387L;
private JTree tree;
private final static String APP_TITLE = "ArchiveViewer";
private JButton btn;
private JTextPane textPane;
private JProgressBar progressBar;
private JFileChooser fileChooser;
private DefaultTreeModel model;
private ZipFile theZipFile;
public ArchiveViewer() {
super(APP_TITLE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel leftPanel = new JPanel(new BorderLayout());
tree = new JTree((TreeModel) null);
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
TreePath path = tree.getSelectionPath();
if (path == null)
return;
tree.scrollPathToVisible(path);
Object[] elements = path.getPath();
String fileName = elements[elements.length - 1].toString();
if (!fileName.endsWith(".java"))
return;
StringBuilder builder = new StringBuilder();
for (int i = 1; i < elements.length - 1; i++) {
builder.append(elements[i]);
builder.append('/');
}
builder.append(fileName);
ZipEntry entry = findEntryByName(builder.toString());
if (entry == null)
return;
try {
textPane.read(theZipFile.getInputStream(entry), fileName);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btn = new JButton("select archive");
progressBar = new JProgressBar();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fileChooser == null) {
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String name = f.getName();
return name.matches(".*.(zip|jar)");
}
public String getDescription() {
return "Supported Archiv extensions (zip,jar)";
}
});
}
if (fileChooser.showOpenDialog(ArchiveViewer.this) == JFileChooser.APPROVE_OPTION) {
final File file = fileChooser.getSelectedFile();
if (file.isFile()) {
System.out.println(file);
new Thread() {
{
setPriority(MIN_PRIORITY);
}
public void run() {
btn.setEnabled(false);
setTitle(APP_TITLE + ": " + file.getName());
processArchive(file);
btn.setEnabled(true);
}
}.start();
}
}
}
});
Container c = getContentPane();
leftPanel.add(new JScrollPane(tree) {
{
setPreferredSize(new Dimension(180, 400));
}
}, BorderLayout.NORTH);
leftPanel.add(btn, BorderLayout.CENTER);
leftPanel.add(progressBar, BorderLayout.SOUTH);
textPane = new JTextPane() {
public void setSize(Dimension d) {
if (d.width < getParent().getSize().width)
d.width = getParent().getSize().width;
super.setSize(d);
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
};
splitPane.add(leftPanel);
splitPane.add(new JPanel(new BorderLayout()) {
{
setPreferredSize(new Dimension(500, 400));
add(new JScrollPane(textPane), BorderLayout.CENTER);
}
});
c.add(splitPane);
pack();
setVisible(true);
}
/**
* @param file
*/
protected void processArchive(File file) {
try {
model = new ArchiveTreeModel(new ZipFile(file));
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tree.setModel(model);
}
public static void main(String[] args) {
new ArchiveViewer();
}
private ZipEntry findEntryByName(String name) {
ZipEntry entry = null;
for (Enumeration e = theZipFile.entries(); e
.hasMoreElements();) {
entry = (ZipEntry)e.nextElement();
if (entry.getName().equals(name)) {
return entry;
}
}
return null;
}
class ArchiveTreeModel extends DefaultTreeModel {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3258689897023093049L;
public ArchiveTreeModel(ZipFile zipFile) {
super(new DefaultMutableTreeNode("/"));
/* */
theZipFile = zipFile;
progressBar.setMinimum(0);
progressBar.setMaximum(zipFile.size());
int value = 0;
/* */
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String name = entry.getName();
String[] elements = name.split("/");
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getRoot();
/* */
progressBar.setValue(++value);
/* */
for (int i = 0; i < elements.length; i++) {
String token = elements[i];
DefaultMutableTreeNode nextNode = findNode(currentNode,
token);
if (nextNode == null) {
nextNode = new DefaultMutableTreeNode(token);
currentNode.add(nextNode);
}
currentNode = nextNode;
}
}
}
private DefaultMutableTreeNode findNode(DefaultMutableTreeNode parent,
String name) {
Enumeration e = parent.children();
while (e.hasMoreElements()) {
DefaultMutableTreeNode element = (DefaultMutableTreeNode) e
.nextElement();
if (element.getUserObject().equals(name)) {
return element;
}
}
return null;
}
}
}