private static final int BSIZE = 1024 * 1000 ; /** * @param args */ public static void main(String[] args)throws Exception{ FileOutputStream file = new FileOutputStream("E://he.zip");
//Adler32(更快一些)和CRC32(慢一点但比较准确) CheckedOutputStream cOut = new CheckedOutputStream(file,new Adler32()); ZipOutputStream zipOut = new ZipOutputStream(cOut); BufferedOutputStream out = new BufferedOutputStream(zipOut); zipOut.setComment("this is a java zipping");
//写两个同样的文件进入 for(int i=0;i<2;i++) { FileChannel in = new FileInputStream("E://a.txt").getChannel(); zipOut.putNextEntry(new ZipEntry("c"+i+".txt")); ByteBuffer buff = ByteBuffer.allocate(BSIZE);
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/**
* @author dqhe
* 压缩zip
* 解压zip
*/
public class ZipCompress {
private static final int BSIZE = 1024 * 1000 ;
/**
* @param args
*/
public static void main(String[] args)throws Exception{
FileOutputStream file = new FileOutputStream("E://he.zip");
//Adler32(更快一些)和CRC32(慢一点但比较准确)
CheckedOutputStream cOut = new CheckedOutputStream(file,new Adler32());
ZipOutputStream zipOut = new ZipOutputStream(cOut);
BufferedOutputStream out = new BufferedOutputStream(zipOut);
zipOut.setComment("this is a java zipping");
//写两个同样的文件进入
for(int i=0;i<2;i++)
{
FileChannel in = new FileInputStream("E://a.txt").getChannel();
zipOut.putNextEntry(new ZipEntry("c"+i+".txt"));
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
//BufferedReader in = new BufferedReader(new FileReader("E://c.txt"));
//int c;
System.out.println("正在写入,请稍等……");
while(in.read(buff) != -1)
{
System.out.println(in.read(buff));
buff.flip();
out.write(buff.array());
buff.clear();
}
System.out.println("成功写入。");
}
out.close();
zipOut.close();
cOut.close();
} }
以前写的楼主可以参考下压缩的,密码没写过
有可能就加一个属性吧,呵呵
package meadproperty.util;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;public class ZipUtil { public static int BUFFER_BYTE = 512; private static String BASE_DIR = "";
private static byte BYTES[] = new byte[BUFFER_BYTE]; public static void zipDir(String src_dir) throws IOException {
zipDir(src_dir, src_dir + ".zip");
} /**
*
* @param src_dir
* @param dest_zip
* @throws IOException
*/
public static void zipDir(String src_dir, String dest_zip)
throws IOException { FileOutputStream fos = new FileOutputStream(dest_zip);
ZipOutputStream zout = new ZipOutputStream(fos);
File file = new File(src_dir);
if (!file.exists()) {
// src not available...
System.err.println(file.getName() + " is not available!");
return;
} else if (!file.isDirectory()) {
zip(zout, file, file.getPath());
System.err.println(file.getName()
+ "#is file, not dir!warn, but no error caused!");
}
zip(zout, file, ""); zout.close();
fos.close();
} /**
*
* @param zipOut
* @param file
* @param base
* @throws IOException
*/
protected static void zip(ZipOutputStream zipOut, File file, String base)
throws IOException {
if (file.isDirectory()) {
// process dir
File[] fl = file.listFiles();
zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(zipOut, fl[i], base + fl[i].getName());
}
} else {
// process file
zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
InputStream in = new FileInputStream(file);
System.out.println(base);
int len = 0;
while ((len = in.read(BYTES)) != -1) {
zipOut.write(BYTES, 0, len);
}
in.close();
}
} /**
* unZip the file
*
* @param fileName
* @throws IOException
*/
public static void unzipFile(String src_zip) throws IOException {
int i = src_zip.lastIndexOf('.');
String dest = src_zip.substring(0, i);
unzipFile(src_zip, dest);
} /**
*
* @param src_zip
* @param dest_dir
* @throws IOException
*/
public static void unzipFile(String src_zip, String dest_dir)
throws IOException {
ZipUtil.BASE_DIR = dest_dir;
File base = new File(ZipUtil.BASE_DIR);
if (!base.exists()) {
base.mkdirs();
}
InputStream in = new BufferedInputStream(new FileInputStream(src_zip));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
try {
unzip(zin, ZipUtil.BASE_DIR + File.separator + entry.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
zin.close();
} /**
*
* @param zin
* @param entryName
* the spe file entry in the zip file
* @throws IOException
*/
protected static void unzip(ZipInputStream zin, String entryName)
throws IOException {
System.out.println("unzipping " + entryName);
// create dir
if (entryName.endsWith("/")) {
System.out.println("INFO: #endsWith/,will return now");
return;
}
int i = entryName.lastIndexOf('/');
if (i > 0) {
String path = entryName.substring(0, i);
File file = new File(path);
if (!file.exists()) {
System.out.println(file.getName()
+ "INFO: #not existed, will create one");
file.mkdirs();
}
System.out.println(path + " =file= " + i);
}
// create file
FileOutputStream out = new FileOutputStream(entryName);
int len = 0;
while ((len = zin.read(BYTES)) != -1) {
out.write(BYTES, 0, len);
}
out.close();
}}
支持多级目录..多个文件
直接用7zip的dll调用就好了
cmd命令行,在java里面调用...呵呵原理一样的