//Pseudo-Code
if(file.toString().endsWith("zip")){
ZipInputStream zip = new ZipInpuStream(new FileInputStream(file));
}
else if(file.toString().endsWith("gz")){
GZIPInputStream zip = new GZIPInpuStream(new FileInputStream(file));
}
import java.io.*;
import javax.swing.JOptionPane;
import java.util.zip.*;
class DeComprimize {
File files[];
GUI frame;
DeComprimize(File files[], GUI frame) throws IOException{
this.files = files;
this.frame = frame;
decomprimize();
}
private void decomprimize() throws IOException{
for(byte i = 0; i < files.length; i++){
if (files[i].exists() && files[i].toString().endsWith("gz")) {
int laenge = files[i].toString().length() - 3;
String name = files[i].toString().substring(0, laenge);
byte buffer[] = new byte[6000];
int gelesen;
FileOutputStream output = new FileOutputStream(name);
FileInputStream input = new FileInputStream(files[i]);
if(files[i].toString().endsWith("zip"))
GZIPInputStream gzipIn = new GZIPInputStream(input);
else
ZipInputStream gzipIn = new ZipInputStream(input);
while ( (gelesen = gzipIn.read(buffer, 0, buffer.length)) != -1)
output.write(buffer, 0, gelesen);
JOptionPane.showMessageDialog(frame,
files[i].toString() + " erfolgreich entpackt");
output.close();
gzipIn.close();
input.close();
}
else
JOptionPane.showMessageDialog(frame, "Falsches Format!");
}
}
}
if (files[i].exists() && files[i].toString().endsWith("gz") || files[i].toString().endsWith("zip"))
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener{
JPanel contentPane = new JPanel();
JMenuBar mBar = new JMenuBar();
JButton btEntpacken = new JButton("Entpacken");
JButton btPacken = new JButton("Packen");
JMenu datei = new JMenu("Datei");
JMenuItem decomp = new JMenuItem("EntPacken");
JMenuItem comp = new JMenuItem("Packen");
JMenuItem close = new JMenuItem("Schließen");
JComboBox method = new JComboBox();
public GUI(){
contentPane.add(btEntpacken);
contentPane.add(btPacken);
contentPane.add(method);
btEntpacken.addActionListener(this);
btPacken.addActionListener(this);
decomp.addActionListener(this);
comp.addActionListener(this);
close.addActionListener(this);
setJMenuBar(mBar);
setContentPane(contentPane);
datei.add(decomp);
datei.add(comp);
datei.add(close);
method.addItem("gzip");
method.addItem("zip");
mBar.add(datei);
}
public void actionPerformed(ActionEvent ae){
File files[];
if(ae.getSource() == close)
System.exit(0);
else{
JFileChooser fd = new JFileChooser();
fd.setMultiSelectionEnabled(true);
int action = fd.showOpenDialog(this);
if(action == JFileChooser.APPROVE_OPTION){ //falls nicht abgebrochen wurde...
files = fd.getSelectedFiles();
if(ae.getSource() == comp || ae.getSource() == btPacken){
try {
new Comprimize(files, "GZIP", this);
}
catch (IOException e) {}
}
else if(ae.getSource() == decomp || ae.getSource() == btEntpacken){
try{
new DeComprimize(files, this);
}catch(IOException e){}
}
}
}
}
public static void main(String args[]){
JFrame frame= new GUI();
frame.setSize(300, 100);
frame.setTitle("Das ultimative Entpackungsprogramm");
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.setVisible(true);
frame.setResizable(false);
}
}
/*class OnlyZIP implements FileFilter{
public boolean accept(File file){
return file.toString().toLowerCase().endsWith(".gz");
}
}*/
import java.io.*;
import javax.swing.JOptionPane;
import java.util.zip.*;
class DeComprimize {
File files[];
GUI frame;
DeComprimize(File files[], GUI frame) throws IOException{
this.files = files;
this.frame = frame;
decomprimize();
}
private void decomprimize() throws IOException{
for(byte i = 0; i < files.length; i++){
if (files[i].exists() && files[i].toString().endsWith("gz")) {
int laenge = files[i].toString().length() - 3;
String name = files[i].toString().substring(0, laenge);
byte buffer[] = new byte[6000];
int gelesen;
FileOutputStream output = new FileOutputStream(name);
FileInputStream input = new FileInputStream(files[i]);
if(files[i].toString().endsWith("zip"))
GZIPInputStream gzipIn = new GZIPInputStream(input);
else
ZipInputStream gzipIn = new ZipInputStream(input);
while ( (gelesen = gzipIn.read(buffer, 0, buffer.length)) != -1)
output.write(buffer, 0, gelesen);
JOptionPane.showMessageDialog(frame,
files[i].toString() + " erfolgreich entpackt");
output.close();
gzipIn.close();
input.close();
}
else
JOptionPane.showMessageDialog(frame, "Falsches Format!");
}
}
}
InflaterInputStream gzipIn = null;
if(files[i].toString().endsWith("zip"))
gzipIn = new GZIPInputStream(input);
else
gzipIn = new ZipInputStream(input);
import java.io.*;
import java.util.zip.*;
import javax.swing.JOptionPane;
class Comprimize{
File files[];
GUI frame;
String type;
Comprimize(File files[], String type, GUI frame) throws IOException{
this.files = files;
this.type = (type.equals("zip"))? "zip" : "gz";
this.frame = frame;
comprimize();
}
private void comprimize() throws IOException{
byte buffer[] = new byte[6000];
int gelesen = 0;
for(byte i = 0; i < files.length; i++){
if (files[i].exists() && files[i].isFile()) {
FileInputStream input = new FileInputStream(files[i]);
FileOutputStream output = new FileOutputStream(
files[i].toString() + "." + type);
DeflaterOutputStream gzipOut;
if(type.equals("zip"))
gzipOut = new ZipOutputStream(output);
else
gzipOut = new GZIPOutputStream(output);
while ( (gelesen = input.read(buffer, 0, buffer.length)) != -1)
gzipOut.write(buffer, 0, gelesen);
gzipOut.close();
output.close();
input.close();
JOptionPane.showMessageDialog(frame,
files[i].toString() + " erfolgreich gepackt");
}
else {
JOptionPane.showMessageDialog(frame, "Fehler!",
files[i].toString() + "ist ungültig",
JOptionPane.ERROR_MESSAGE);
}
}
}
}