sourEntry= new ZipEntry(file.getName()); FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); jos.putNextEntry(sourEntry); int len; while ((len = in.read(buf)) >= 0) jos.write(buf, 0, len); in.close(); jos.closeEntry();
} public File getSourceFile() { return sourceFile; } public void setSourceFile(File sourceFile) { this.sourceFile = sourceFile; } public String getZipfileName() { return zipfileName; } public void setZipfileName(String zipfileName) { this.zipfileName = zipfileName; }
public class DirectoryZip { public static void jar(String inputFileName, String outputFileName) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName)); out.setEncoding("GBK"); // ###### 这句话是关键,指定输出的编码方式 File f = new File(inputFileName); jar(out, f, ""); out.close(); }
private static void jar(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { File[] fl = f.listFiles(); base = base.length() == 0 ? "" : base + "/"; out.putNextEntry(new ZipEntry(base)); for (int i = 0; i < fl.length; i++) { jar(out, fl[i], base + fl[i].getName()); } } else { out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); byte[] buffer = new byte[1024]; int n = in.read(buffer); while (n != -1) { out.write(buffer, 0, n); n = in.read(buffer); } in.close(); } }
import java.util.zip.*;
import java.io.*;
public class ZipCommand {
private static int ziplevel = 7;
private File sourceFile = null;
private File zipFile = null;
private ZipOutputStream jos = null;
private ZipEntry sourEntry = null;
private String zipfileName = null;
private String sourceFileName = null;
private static byte[] buf = new byte[1024];
public static void main(String[] s){
ZipCommand tz = new ZipCommand();
//System.out.println( tz.AddtoZip("E:/cis-2000/document/CPoPUserSManual_CPoP_User_s_Manual.doc"));
}
public String AddtoZip(List sourcefilelist){
if(sourcefilelist == null || sourcefilelist.size()<1){
return null;
}
sourceFile = new File(sourcefilelist.get(0).toString());
if(!sourceFile.isFile()){
return null;
}else{
sourceFileName = sourceFile.getName();
this.setZipfileName(sourceFileName.substring(0,sourceFileName.lastIndexOf("."))+".zip");
try{
zipFile = new File(sourceFile.getParent(),this.getZipfileName()); if(zipFile.exists()){
int i = 1 ;
while(true){
zipFile = new File(sourceFile.getParent(),this.getZipfileName().substring(0, getZipfileName().lastIndexOf(".zip")) + i + ".zip");
if(!zipFile.exists()) break ;
i++ ;
}
}
//System.out.println(zipFile.getPath());
if(zipFile.exists()){
zipFile.deleteOnExit();
}
zipFile.createNewFile();
jos = new ZipOutputStream(new FileOutputStream(zipFile));
jos.setLevel(ziplevel);
for(int i=0;i<sourcefilelist.size();i++){
Stzip(jos,new File(sourcefilelist.get(i).toString()));
}
jos.finish();
}catch(Exception e){
return null;
}
if(zipFile !=null)
return zipFile.getPath();
else{
return null;
}
}
}
private void Stzip(ZipOutputStream jos, File file)
throws IOException, FileNotFoundException
{
sourEntry= new ZipEntry(file.getName());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
jos.putNextEntry(sourEntry);
int len;
while ((len = in.read(buf)) >= 0)
jos.write(buf, 0, len);
in.close();
jos.closeEntry();
}
public File getSourceFile() {
return sourceFile;
}
public void setSourceFile(File sourceFile) {
this.sourceFile = sourceFile;
} public String getZipfileName() {
return zipfileName;
} public void setZipfileName(String zipfileName) {
this.zipfileName = zipfileName;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class DirectoryZip {
public static void jar(String inputFileName, String outputFileName) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName));
out.setEncoding("GBK"); // ###### 这句话是关键,指定输出的编码方式
File f = new File(inputFileName);
jar(out, f, "");
out.close();
}
private static void jar(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base.length() == 0 ? "" : base + "/";
out.putNextEntry(new ZipEntry(base));
for (int i = 0; i < fl.length; i++) {
jar(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[1024];
int n = in.read(buffer);
while (n != -1) {
out.write(buffer, 0, n);
n = in.read(buffer);
}
in.close();
}
}
public static void main(String[] args) {
try {
jar("D:\\temp\\1", "d://mytest.zip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;public class ZipUtil { /**
* 压缩。
*
* @param src
* 源文件或者目录
* @param dest
* 压缩文件路径
* @throws IOException
*/
public static void zip(String src, String dest) throws IOException {
ZipOutputStream out = null;
try {
File outFile = new File(dest);
out = new ZipOutputStream(outFile);
File fileOrDirectory = new File(src); if (fileOrDirectory.isFile()) {
zipFileOrDirectory(out, fileOrDirectory, "");
} else {
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], "");
}
} } catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
} /**
* 递归压缩文件或目录
*
* @param out
* 压缩输出流对象
* @param fileOrDirectory
* 要压缩的文件或目录对象
* @param curPath
* 当前压缩条目的路径,用于指定条目名称的前缀
* @throws IOException
*/
private static void zipFileOrDirectory(ZipOutputStream out,
File fileOrDirectory, String curPath) throws IOException {
FileInputStream in = null;
try {
if (!fileOrDirectory.isDirectory()) {
// 压缩文件
byte[] buffer = new byte[4096];
int bytes_read;
in = new FileInputStream(fileOrDirectory); ZipEntry entry = new ZipEntry(curPath
+ fileOrDirectory.getName());
out.putNextEntry(entry); while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
} else {
// 压缩目录
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], curPath
+ fileOrDirectory.getName() + "/");
}
}
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
}
} /**
* 解压缩。
*
* @param zipFileName
* 源文件
* @param outputDirectory
* 解压缩后文件存放的目录
* @throws IOException
*/
public static void unzip(String zipFileName, String outputDirectory)
throws IOException { ZipFile zipFile = null; try {
zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.getEntries(); ZipEntry zipEntry = null; File dest = new File(outputDirectory);
dest.mkdirs(); while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement(); String entryName = zipEntry.getName(); InputStream in = null;
FileOutputStream out = null; try {
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator
+ name);
f.mkdirs();
} else {
int index = entryName.lastIndexOf("\\");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
index = entryName.lastIndexOf("/");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
} File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
// f.createNewFile();
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f); int c;
byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
}
} catch (IOException ex) {
ex.printStackTrace();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
} } catch (IOException ex) {
ex.printStackTrace();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ex) {
}
}
} } public static void main(String[] args) {
try {
ZipUtil.zip("C:\\0115080120081024","d:\\0115080120081027.zip");
//ZipUtil.unzip("D:\\a\\010108012008-8-25.zip", "d:\\a\\tmp");
} catch (Exception e) {
e.printStackTrace();
} }}
找到以后删除多余的,最终也就70kb左右,支持zip,tar,gzip