import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipTest {
private static String root;
public static void zipFile(File file) {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(new File
("E:\\桌面\\"
+ file.getName() + ".zip")));
zipFile(file, out);
} catch (Exception e) {
System.out.println("出现异常");
} finally {
if (out != null) {
try {
out.finish();
} catch (IOException e) {
}
}
}
}
public static void zipFile(File file, ZipOutputStream out) throws
Exception {
String path = file.getPath();
int index = path.lastIndexOf(root) + root.length();
if (file.isDirectory()) {
File[] subFile = file.listFiles();
for (File f : subFile) {
zipFile(f, out);
}
} else {
zipFile(file, path.substring(index + 1), out);
}
}
public static void zipFile(File file, String zipPath, ZipOutputStream
out)
throws Exception {
if (file.isDirectory()) {
out.putNextEntry(new ZipEntry(zipPath + "/" +
file.getName() + "/"));
} else {
out.putNextEntry(new ZipEntry(zipPath));
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1000];
int len = -1;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("E:\\桌面\\乱七八糟");
if (file.exists()) {
try {
root = file.getName();
zipFile(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("文件不存在!!!");
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipTest {
private static String root;
public static void zipFile(File file) {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(new File
("E:\\桌面\\"
+ file.getName() + ".zip")));
zipFile(file, out);
} catch (Exception e) {
System.out.println("出现异常");
} finally {
if (out != null) {
try {
out.finish();
} catch (IOException e) {
}
}
}
}
public static void zipFile(File file, ZipOutputStream out) throws
Exception {
String path = file.getPath();
int index = path.lastIndexOf(root) + root.length();
if (file.isDirectory()) {
File[] subFile = file.listFiles();
for (File f : subFile) {
zipFile(f, out);
}
} else {
zipFile(file, path.substring(index + 1), out);
}
}
public static void zipFile(File file, String zipPath, ZipOutputStream
out)
throws Exception {
if (file.isDirectory()) {
out.putNextEntry(new ZipEntry(zipPath + "/" +
file.getName() + "/"));
} else {
out.putNextEntry(new ZipEntry(zipPath));
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1000];
int len = -1;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("E:\\桌面\\乱七八糟");
if (file.exists()) {
try {
root = file.getName();
zipFile(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("文件不存在!!!");
}
}
}