我的代码是:
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.*;
import java.io.*;
import java.util.zip.GZIPOutputStream;
import java.util.*;
import java.awt.Checkbox;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import java.awt.CheckboxGroup;
import java.applet.*;
import javax.swing.JFileChooser;
/*
<applet code="compress1" width=200 height=200>
</applet>
*/
interface CompressionStrategy
{
public void compressFiles(FileOutputStream files,String str);
}
class ZipCompressionStrategy implements CompressionStrategy{
public void compressFiles(FileOutputStream files, String str)
{
byte[] buffer = new byte[1024];
try{
//using ZIP approach
ZipOutputStream zos = new ZipOutputStream(files);
ZipEntry ze= new ZipEntry(str);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(str);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
System.out.println("Done with Zip");
}
catch(IOException ex){
ex.printStackTrace();}
}
}
class GZipCompressionStrategy implements CompressionStrategy
{
public void compressFiles(FileOutputStream files, String str)
{
//using RAR approach
//FileOutputStream fos = null;
GZIPOutputStream gos = null;
FileInputStream fis = null;
try {
// fos = new FileOutputStream("e://myGzip.gzip");
gos = new GZIPOutputStream(files);
fis = new FileInputStream(str);
byte[] tmp = new byte[4*1024];
int size = 0;
while ((size = fis.read(tmp)) != -1) {
gos.write(tmp, 0, size); }
gos.finish();
System.out.println("Done with GZip...");
}
catch (IOException e) {}
finally{
try{
if(fis != null) fis.close();
if(gos != null) gos.close();
} catch(Exception ex){}
}
}
}
class CompressionContext
{
private CompressionStrategy strategy;
//this can be set at runtime by the application preferences
public void setCompressionStrategy(CompressionStrategy strategy)
{
this.strategy = strategy;
}
//use the strategy
public void createArchive(FileOutputStream files, String str)
{
strategy.compressFiles(files,str);
}
}
public class compress1 extends Applet implements ActionListener
{
int n;
Button b1;
Checkbox jcb1,jcb2;
CheckboxGroup cbg;
int r;
JFileChooser fs=new JFileChooser();
File file1;
static String filenm;
//TextField tf;
public void init()
{
//setTitle("compress");
Panel p1;
//setLayout(new FlowLayout());
p1=new Panel();
b1=new Button("compress");
cbg=new CheckboxGroup();
jcb1=new Checkbox("zip1",cbg,true);
jcb2=new Checkbox("gzip1",cbg,false);
//b1.setPreferredSize(new Dimension(100,100));
b1.addActionListener(this);
//tf=new TextField("",6);
p1.add(b1);
//p1.add(tf);
p1.add(jcb1);
p1.add(jcb2);
add(p1,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==b1)
{
r=fs.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
file1=fs.getSelectedFile();
filenm=file1.getAbsolutePath();
}
if(jcb1.getLabel()=="zip1"){
n=1;
}
else if(jcb2.getLabel()=="gzip1"){
n=2;
}
else{}
compress1.x(n);
}
}
static void x(int n)
{
CompressionContext ctx = new CompressionContext();
switch(n)
{
//ctx.setCompressionStrategy(new ZipCompressionStrategy());
case 1:
ctx.setCompressionStrategy(new ZipCompressionStrategy());
//get a list of files
try{
FileOutputStream fos = new FileOutputStream("f:\\MyFile1.zip");
//String st="f:\\MyFile1.zip";
ctx.createArchive(fos,filenm);
}
catch(IOException ex){
ex.printStackTrace();}
break;
case 2:
ctx.setCompressionStrategy(new GZipCompressionStrategy());
//get a list of files
try{
FileOutputStream fos = new FileOutputStream("f:\\MyFile1.gzip");
//String st="f:\\MyFile.zip";
ctx.createArchive(fos,filenm);
}
catch(IOException ex){
ex.printStackTrace();}
break;
case 3:
System.exit(0);
}
}
}
这是压缩文件的代码。当我使用框架时,它正常工作,但当现在我正在使用applet然后对话框没有显示任何路径元素时代替框架,我不知道发生了什么。