import java.io.*; import java.util.*; import java.util.zip.*;//Compress files and folders public class Compressor { /** * This seperator is used for ZipEntr. */ private static final String SEPERATOR = "/"; /** * Use for Public method zip. * @param out * @param f zip file * @param base * @throws java.lang.Exception */ // this Method zip files to the same Stream protected static void zip(ZipOutputStream out, File f, String base) throws Exception { System.out.println(" ... Now Zipping : " + f.getPath()); // one folder if (f.isDirectory()) { File[] fl = f.listFiles(); //put folder entry in out stream out.putNextEntry(new ZipEntry(base + SEPERATOR)); base = base.length() == 0 ? "" : base + SEPERATOR; // zip all files in folder for (int i = 0; i < fl.length; i++) { //Pass and return zip(out, fl[i], base + fl[i].getName()); } } // one file else { out.putNextEntry(new ZipEntry(base)); //put file entry in out stream FileInputStream in = new FileInputStream(f); int buf; // write to zipfile by outStream while ( (buf = in.read()) != -1) { out.write(buf); } in.close(); } } /** * Compress a folder and all its files, or compress one file. * @param zipFileName The zip file name. * @param fromFileName The source folder name or file name. * @throws java.lang.Exception */ public static void zip(String fromFileName, String zipFileName) throws Exception { File fromFile = new File(fromFileName); // create outStream FileOutputStream fout = new FileOutputStream(zipFileName); //CheckedOutputStream csum = new CheckedOutputStream(fout, new CRC32()); //ZipOutputStream out = new ZipOutputStream(csum); ZipOutputStream out = new ZipOutputStream(fout); System.out.println("--- zip start!! ---"); String folder = ""; //file or folder's name by java.lang.String folder we save the route folder = fromFileName.substring(fromFileName.lastIndexOf(SEPERATOR), fromFileName.length()); zip(out, fromFile, folder); System.out.println("--- zip done!! ---"); //update Stream out.flush(); // close Stream out.close(); } /** * Decompress a zip file into a folder. * @param zipFileName The zip file name. * @param outputDirectory The folder contains files decompressed. * @throws java.lang.Exception */ public static void unzip(String zipFileName, String outputDirectory) throws Exception { FileInputStream fin = new FileInputStream(zipFileName); //CheckedInputStream csum = new CheckedInputStream(fin, new CRC32()); //ZipInputStream in = new ZipInputStream(csum); // read zipfile to inputStream ZipInputStream in = new ZipInputStream(fin); ZipEntry z; while ( (z = in.getNextEntry()) != null) { System.out.println(" ... Now unziping : " + z.getName()); // the entry is one folder. if (z.isDirectory()) { String name = z.getName(); name = name.substring(0, name.length() - 1); //you must create folder for the file before unzip the file File f = new File(outputDirectory + File.separator + name); f.mkdir(); } // the entry is one file else { File f = new File(outputDirectory + File.separator + z.getName()); f.createNewFile(); FileOutputStream out = new FileOutputStream(f); int buf; // write to purpose file while ( (buf = in.read()) != -1) { out.write(buf); } out.close(); } } in.close(); } /** * Test method. * @param args * @throws java.lang.Exception */ public static void main(String args[]) throws Exception { // zip("c:/UnZipApplet.class", "f:/aaa.zip"); // zip("c:/怴婯","c:/aaaa.zip"); unzip("f:/aaa.zip", "f:/"); }}
to: dreamno(C_S_D_N) 那代码调试是通过了 不过我在jsp中 老说这类有问题 [javac] class file contains wrong class: Compressor [javac] Please remove or make sure it appears in the correct subdirectory of the classpath. [javac] Compressor compressor =new Compressor(); [javac] ^ [javac] 1 error我这样实例的 Compressor compressor =new Compressor(); compressor.zip("d:/test.txt", "c:/temp/tt.zip");
http://www.easybetter.com/readarticle/htm/41/2004_2_19_232.html
import java.util.*;
import java.util.zip.*;//Compress files and folders
public class Compressor {
/**
* This seperator is used for ZipEntr.
*/
private static final String SEPERATOR = "/"; /**
* Use for Public method zip.
* @param out
* @param f zip file
* @param base
* @throws java.lang.Exception
*/
// this Method zip files to the same Stream
protected static void zip(ZipOutputStream out, File f, String base) throws
Exception {
System.out.println(" ... Now Zipping : " + f.getPath());
// one folder
if (f.isDirectory()) {
File[] fl = f.listFiles();
//put folder entry in out stream
out.putNextEntry(new ZipEntry(base + SEPERATOR));
base = base.length() == 0 ? "" : base + SEPERATOR;
// zip all files in folder
for (int i = 0; i < fl.length; i++) {
//Pass and return
zip(out, fl[i], base + fl[i].getName());
}
}
// one file
else {
out.putNextEntry(new ZipEntry(base));
//put file entry in out stream
FileInputStream in = new FileInputStream(f);
int buf;
// write to zipfile by outStream
while ( (buf = in.read()) != -1) {
out.write(buf);
}
in.close();
} } /**
* Compress a folder and all its files, or compress one file.
* @param zipFileName The zip file name.
* @param fromFileName The source folder name or file name.
* @throws java.lang.Exception
*/
public static void zip(String fromFileName, String zipFileName) throws
Exception {
File fromFile = new File(fromFileName);
// create outStream
FileOutputStream fout = new FileOutputStream(zipFileName);
//CheckedOutputStream csum = new CheckedOutputStream(fout, new CRC32());
//ZipOutputStream out = new ZipOutputStream(csum);
ZipOutputStream out = new ZipOutputStream(fout);
System.out.println("--- zip start!! ---");
String folder = "";
//file or folder's name by java.lang.String folder we save the route
folder = fromFileName.substring(fromFileName.lastIndexOf(SEPERATOR),
fromFileName.length());
zip(out, fromFile, folder);
System.out.println("--- zip done!! ---");
//update Stream
out.flush();
// close Stream
out.close(); } /**
* Decompress a zip file into a folder.
* @param zipFileName The zip file name.
* @param outputDirectory The folder contains files decompressed.
* @throws java.lang.Exception
*/ public static void unzip(String zipFileName, String outputDirectory) throws
Exception {
FileInputStream fin = new FileInputStream(zipFileName);
//CheckedInputStream csum = new CheckedInputStream(fin, new CRC32());
//ZipInputStream in = new ZipInputStream(csum);
// read zipfile to inputStream
ZipInputStream in = new ZipInputStream(fin);
ZipEntry z;
while ( (z = in.getNextEntry()) != null) {
System.out.println(" ... Now unziping : " + z.getName());
// the entry is one folder.
if (z.isDirectory()) {
String name = z.getName();
name = name.substring(0, name.length() - 1);
//you must create folder for the file before unzip the file
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
}
// the entry is one file
else {
File f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int buf;
// write to purpose file
while ( (buf = in.read()) != -1) {
out.write(buf);
}
out.close();
}
} in.close();
} /**
* Test method.
* @param args
* @throws java.lang.Exception
*/
public static void main(String args[]) throws Exception {
// zip("c:/UnZipApplet.class", "f:/aaa.zip");
// zip("c:/怴婯","c:/aaaa.zip");
unzip("f:/aaa.zip", "f:/");
}}
那代码调试是通过了
不过我在jsp中 老说这类有问题
[javac] class file contains wrong class: Compressor
[javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
[javac] Compressor compressor =new Compressor();
[javac] ^
[javac] 1 error我这样实例的
Compressor compressor =new Compressor();
compressor.zip("d:/test.txt", "c:/temp/tt.zip");
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.*;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.InflaterInputStream;
import java.net.URLDecoder;/**
* @author red
* @version 1.0 2004-12-17
*/
public class ShowImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
} public void doProcess(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String name = req.getParameter("name");
name = URLDecoder.decode(name, "UTF-8");
String zipFile = "c:/1.zip";
File file = new File(zipFile);
FileInputStream fis = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = null; do { entry = zis.getNextEntry();
if (entry == null)
continue; if (!entry.getName().equals(name)) {
continue;
}
byte[] buf = getData(zis);
OutputStream toClient = res.getOutputStream(); //得到向客户端输出二进制数据的对象
toClient.write(buf); //输出数据
toClient.close();
break;
} while (entry != null); zis.close();
fis.close();
} private byte[] getData(InflaterInputStream zis) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
byte[] buf = null;
int length = 0; while ((length = zis.read(temp, 0, 1024)) != -1) {
bout.write(temp, 0, length);
} buf = bout.toByteArray();
bout.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipManager
{
public static final int BUFFER_SIZE = 1024;
public static final String FS = System.getProperty("file.separator");
private String archiveRoot = System.getProperty("user.dir"); public void setArchiveRoot( String directory )
{
this.archiveRoot = directory;
} public String getArchiveRoot()
{
return archiveRoot;
} public void doCompress( File file, File toCreate )
throws IOException
{
if( toCreate.exists() )
throw new IOException( toCreate + " is already exist" ); FileOutputStream fos = new FileOutputStream( toCreate );
doCompress( file, fos );
} public void doCompress( File file, OutputStream out )
throws IOException
{
ZipOutputStream zos = new ZipOutputStream( out ); compress( file, zos ); zos.setMethod( ZipOutputStream.DEFLATED );
zos.close();
} private void compress( File file, ZipOutputStream zos )
throws IOException
{
if( file.isDirectory() )
{
File[] files = file.listFiles();
for(int i=0; i<files.length; i++)
{
if( files[i].isDirectory() )
compress( files[i], zos );
else
addFile( files[i], zos );
}
}
else
if( file.isFile() )
{
addFile( file, zos );
}
} public void addFile( File file, ZipOutputStream zos )
throws IOException
{
if( Thread.currentThread().isInterrupted() )
return; compressStarted( file ); String enname = file.getAbsolutePath().substring(archiveRoot.length()+1);
ZipEntry en = new ZipEntry( enname );
CRC32 crc32 = new CRC32(); byte[] chs = new byte[1024]; FileInputStream fis = new FileInputStream(file);
int len = 0;
while( (len=fis.read( chs )) > -1 )
crc32.update( chs, 0, len );
fis.close(); en.setSize( file.length() );
en.setTime( file.lastModified() );
en.setCrc( crc32.getValue() ); zos.putNextEntry( en );
fis = new FileInputStream(file);
while( (len=fis.read( chs )) > -1 )
zos.write( chs, 0, len );
fis.close();
zos.closeEntry(); compressComplete( file );
}