Sie sind auf Seite 1von 13

import java.io.

*; /* import packages*/
import java.awt.*; /* import packages*/
import java.awt.event.*; /* import packages*/
import javax.swing.*; /* import packages*/
import javax.swing.table.*; /* import packages*/
import java.util.*; /* import packages*/
import java.util.zip.*; /* import packages*/
import java.util.Date;
import java.util.zip.ZipFile;
class FileZipper extends JFrame implements ActionListener /* we extend frame
so we don`
t create
object of
frame*/
{
/** here we declare instance variable so we access
* these variable any where in main method**/
JButton but1;
JButton but2;
JButton but3;
JButton but4;
JButton but5;
JLabel l;

JTable j;
DefaultTableModel dtm;
JTableHeader jth;
File openedFile;
Vector listofFile;
Vector listofName;
String filename="";
FileZipper()
{ listofFile= new Vector();
listofName= new Vector();
setLayout(new BorderLayout());
dtm=new DefaultTableModel(0,0);
j = new JTable(dtm);
j.setEnabled(false);
dtm.addColumn("Name");
dtm.addColumn("Size");
dtm.addColumn("Modified");
dtm.addColumn("CompressedSize");
dtm.addColumn("Ratio");
dtm.addColumn("path");
dtm.addColumn("Crc");


j.setBackground(Color.ORANGE);
j.setForeground(Color.BLACK);
JScrollPane js=new JScrollPane(j);
add(js,BorderLayout.CENTER);
JPanel toolbar = new JPanel();
but1 = new JButton("New");
but2 = new JButton("Open");
but3 = new JButton("Extract");
but4 = new JButton("Add");
but5 = new JButton("Close");

but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
toolbar.setLayout(new GridLayout(5,1,10,10));
toolbar.add(but1);
toolbar.add(but2);
toolbar.add(but3);
toolbar.add(but4);
toolbar.add(but5);

toolbar.setForeground(Color.BLACK);
but1.setBackground(Color.WHITE);
but2.setBackground(Color.WHITE);
but3.setBackground(Color.WHITE);
but4.setBackground(Color.WHITE);
but5.setBackground(Color.WHITE);



add(toolbar,BorderLayout.WEST);
JPanel north=new JPanel();
north.setLayout(new FlowLayout());
JLabel status = new JLabel("zipper file");

status.setForeground(Color.DARK_GRAY);
status.setFont(new Font("Times New Roman",Font.ITALIC,48
));
status.setBackground(Color.ORANGE);
north.add(status);
add(north,BorderLayout.NORTH);


//but1.disable();

//setBackground(Color.green);
addWindowListener(new MyActionListener() );
l= new JLabel();
add(l,BorderLayout.SOUTH);
setSize(300,300);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
public static void main(String args[])
{
new FileZipper();
}

public void actionPerformed(ActionEvent ae)


{

JButton b =(JButton)ae.getSource();
if(b==but1)
{
JFileChooser jf=new JFileChooser();
int res=jf.showSaveDialog(this);
if(res == jf.APPROVE_OPTION)
{
openedFile = jf.getSelectedFile(
);

zipFile();
}
}
else if(b==but2)
{

JFileChooser jf=new JFileChooser();
jf.setFileFilter(new javax.swing.filech
ooser.FileFilter()
{ public boolean accept(File f)
{ if(f.isDirectory())
{
return true;
}
String name = f.getName(
);
String extension = name.
substring(name.lastIndexOf("."));
if(extension.equalsIgnor
eCase(".zip") || extension.equalsIgnoreCase(".jar"))
{
return true;
}
else
{
return false;
}
}
public String getDescription()
{
return "zip and jar file
s";
}
});
jf.setMultiSelectionEnabled(true);
int res=jf.showOpenDialog(this);
if(res == jf.APPROVE_OPTION)

{
openedFile = jf.getSelectedFile();
openFile();

}
}
else if(b==but3)

{
extractFile();

}
else if(b==but4)

{


zipFileAfterOpen();
}

else if(b==but5)
{
this.dispose();
}
}
void zipFile()
{
JFileChooser jfopen=new JFileChooser();

jfopen.setMultiSelectionEnabled(true);
jfopen.setFileSelectionMode(JFileChooser.FILE
S_AND_DIRECTORIES);
int r=jfopen.showOpenDialog(this);
File[] selectedFiles;
if(r == jfopen.APPROVE_OPTION)
{
selectedFiles=jfopen.get
SelectedFiles();
}
else
{
return;
}
int i,p=1;
FileInputStream fin = null;
FileOutputStream fout=null;
ZipOutputStream zout=null;


try
{
fout = new FileOutputStream(open
edFile);
zout = new ZipOutputStream(new B
ufferedOutputStream(fout));
for(int j=0;j<selectedFiles.length;j
++)

{ filename=selectedFiles[j
].getName();
zipFile1(selectedFiles[j
]);
}
for(int counter=0;counter<listofFile.siz
e();counter++)
{

File myfile=(File)listofFile.g
et(counter);
try

{
fin = new FileInputStrea
m(myfile);
}
catch(FileNotFoundException e)

{
System.out.println(e);
e.printStackTrace();
return;
}
String n = (String)listo
fName.get(counter);
ZipEntry ze = new ZipEntry(n);

zout.putNextEntry(ze);
byte b[]=new byte[4096];
i = fin.read(b);
while(i!=(-1))

{
zout.write(b,0,i);
i=fin.read(b);
}
fin.close();
}
zout.close();
fout.close();
clearTable();
openFile();
String msg="compression Of File
"+" Is Completed.";
JOptionPane.showMessageDialog(this,m
sg,"Information",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
}
}

void zipFile1(File file)

{
if(file.isDirectory())
{
File fileArray[]=file.li
stFiles();
for(int i=0;i<fileArray.leng
th;i++)
{
zipFile1(fileArray[i]);
}
}
else
{
listofFile.add(file);
String path= file.getAbsolutePat
h();
path = path.substring(path.index
Of(filename));
listofName.add(path);
}

}

void zipFileAfterOpen()
{ try
{

JFileChooser jfopen=new JFileChooser();
jfopen.setMultiSelectionEnabled(true);
jfopen.setFileSelectionMode(JFileChooser.FILE
S_AND_DIRECTORIES);
int r=jfopen.showOpenDialog(this);
File[] selectedFiles;
File tmpFile=null;
if(r == jfopen.APPROVE_OPTION)
{
selectedFiles=jfopen.get
SelectedFiles();
}
else
{
return;
}
int i,p=1;
FileInputStream fin = null;
FileOutputStream fout=null;
ZipOutputStream zout=null;

tmpFile = new File(openedFile.getParentFile(),"temp.
zip");
fout = new FileOutputStream(tmpFile);
zout = new ZipOutputStream(new BufferedO
utputStream(fout));
listofFile.clear();
listofName.clear();
for(int j=0;j<selectedFiles.length;j++)
{ filename=selectedFiles[j].getNam
e();
zipFile1(selectedFiles[j]);
}
ZipInputStream zis;
zis = new ZipInputStream(new FileInputSt
ream(openedFile));
ZipEntry ze1 = null;
while((ze1 = zis.getNextEntry())!=null)

{ try{
String name=ze1.getName(
);
if(!ze1.isDirectory())
{
try{zout.putNext
Entry(ze1);}catch(Exception e){continue;}
}
else
{
continue;
}
byte b[]=new byte[4096];
i = zis.read(b);
while(i!=(-1))
{
zout.write(b,0,i
);
i = zis.read(b);
}
}
catch(Exception e)
{
e.printStackTrac
e();
}
}
zis.close();
for(int counter=0;counter<listofFile.size();coun
ter++)
{ try

{
File myfile=(File)listofFile.g
et(counter);
fin = new FileInputStrea
m(myfile);
String n = (String)listo
fName.get(counter);
ZipEntry ze = new ZipEntry(n);

try{zout.putNextEntry(ze
);}catch(Exception e){continue;}
byte b[]=new byte[4096];
i = fin.read(b);
while(i!=(-1))
{
zout.write(b,0,i
);
i=fin.read(b);
}
fin.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
zout.close();
fout.close();
File prevFile = openedFile;
System.out.println (openedFile.d
elete());
System.out.println (tmpFile.rena
meTo(openedFile));
openedFile = prevFile;
System.out.println ("Opened File
"+openedFile.getName());
clearTable();
openFile();
String msg="compression Of Files
"+" Is Completed.";
JOptionPane.showMessageDialog(this,m
sg,"Information",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
}
}


void extractFile()
{
try
{
File destinationFile;
JFileChooser jf1=new JFileChooser();
jf1.setFileSelectionMode(JFileChooser.DIRECTORI
ES_ONLY);
int res1=jf1.showOpenDialog(this);
if(res1 == jf1.APPROVE_OPTION);
{
destinationFile = jf1.getSelectedFile();
int i;
FileOutputStream fout;
File f2;
ZipInputStream zis;
ZipFile zf = new ZipFile(openedFile);
zis = new ZipInputStream(new FileInputSt
ream(openedFile));
ZipEntry ze = null;
try
{
while((ze = zis.getNextEntry())!
=null)

{
String path= des
tinationFile.getPath();
f2 = new File(pa
th+"/"+ze.getName());
File f3 = f2.get
ParentFile();
f3.mkdirs();
if(!ze.isDirecto
ry())
{
fout = n
ew FileOutputStream(f2);
}
else
{
continue
;
}
byte b[]=new byt
e[4096];
i = zis.read(b);
while(i!=(-1))
{
fout.wri
te(b,0,i);
i = zis.
read(b);
}
fout.close();
}
String msg="Extraction Of File "
+openedFile.getName()+" Is Completed.";
JOptionPane.showMessageDialog(th
is,msg,"Information",JOptionPane.INFORMATION_MESSAGE);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
return;
}
zis.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
void openFile()
{
clearTable();
ZipFile zf=null;
/** use to print file name**/
String n1=openedFile.getName();
l.setText(n1);

try
{
zf=new ZipFile(o
penedFile);
Enumeration e = zf.entries();
while (e.hasMoreElements())
{
ZipEntry ze = (ZipEntry)
e.nextElement();
if(!ze.isDirectory())
{
String name=ze.getName()
;
String size=String.value
Of(ze.getSize());
String modified = new Da
te(ze.getTime()).toString();
String compSize = String
.valueOf(ze.getCompressedSize());
/* to find ratio*/
long g =ze.getSize()-ze
.getCompressedSize();
double g1 = (g*1.0)/ze.
getSize();
String ratio=null;
if(g1>0)
ratio=String.valueOf(((i
nt)(g1*100))+"%");
else
ratio=String.valueOf((0)
+"%");
/*to find value of crc*/
String crc=String.valueO
f(ze.getCrc());
/** to print name & path
*/
String fn1= null;
String path= null;



if(name.contains("\\"))
{
if(name.lastInde
xOf("\\")!=-1)
{

path= name.substring(0,name.lastIndexOf("\\"));

name= name.substring(name.lastIndexOf("\\")+1);

}
}

else if(name.contains
("/"))
{
if(name.
lastIndexOf("/")!=-1)
{

path= name.substring(0,name.lastIndexOf("/"));

name= name.substring(name.lastIndexOf("/")+1);

}
}


String s[]= {nam
e,size,modified,compSize,ratio,path,crc};
dtm.addRow(s);


}


}
zf.close();
}
catch(Exception e)
{
System.out.println("Zip Exception Occur"
+e);
}
}
void clearTable()
{
while(dtm.getRowCount() > 0)
{
dtm.removeRow(0);
}
}
}
class MyActionListener extends WindowAdapter
{
public void windowClosing(WindowEvent ae)
{
Frame f= (Frame)ae.getWindow();
f.dispose();
}
}

Das könnte Ihnen auch gefallen