打了一个ZIP包,但是现在想把这个ZIP包当成一个文件,再打进一个ZIP包?
然后在递归的解开!
下面是单层的源码,但是我想了很久也不知道怎么能变成上面想要的。希望得到大家的帮助啊。
同时提前祝大家节日快乐啊。import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;public class Test {
public static final String zipFileName = "c:/test.zip";
public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
Person p = new Person("张三", 25);
FileOutputStream fileOutStream = new FileOutputStream("zhangsan");
ObjectOutputStream objectOutStream = new ObjectOutputStream(
fileOutStream);
objectOutStream.writeObject(p);
objectOutStream.close();
p = null; FileInputStream fileInStream = new FileInputStream("zhangsan");
ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(
zipFileName));
zipOutput.putNextEntry(new ZipEntry("zhangsan"));
// 将对象变成文件然后压缩成zip
addFile(zipOutput, fileInStream);
zipOutput.close();
// 将zip解压还原出对象
unzip(new File(zipFileName));
System.out.println("end"); } /**
* 释放文件
*/
private static void extractFile(ZipInputStream zipIns, ZipEntry zipEntry)
throws IOException, ClassNotFoundException {
FileOutputStream ous = new FileOutputStream("tmp");
byte[] tmp = new byte[1024];
int len = 0;
while ((len = zipIns.read(tmp)) != -1)
ous.write(tmp, 0, len);
ous.close();
zipIns.closeEntry();
FileInputStream fileInStream = new FileInputStream("tmp");
ObjectInputStream objectInStream = new ObjectInputStream(fileInStream);
Person p = (Person) objectInStream.readObject();
} /**
* 解压缩
*/
public static void unzip(File input) throws IOException,
ClassNotFoundException { ZipInputStream zipIns = new ZipInputStream(new FileInputStream(input));
ZipEntry zipEntry = zipIns.getNextEntry(); while (zipEntry != null) {
extractFile(zipIns, zipEntry); zipEntry = zipIns.getNextEntry();
}
zipIns.close();
}
/**
* 压缩文件
*/
private static void addFile(ZipOutputStream zipOutput, FileInputStream ins)
throws IOException {
byte[] tmp = new byte[1024];
int len = 0;
while ((len = ins.read(tmp)) != -1)
zipOutput.write(tmp, 0, len);
ins.close();
}}class Person implements Serializable { private String name;
private int age; public Person(String name, int age) {
this.name = name;
this.age = age;
} public int getAge() {
return age;
} public String getName() {
return name;
}
}
然后在递归的解开!
下面是单层的源码,但是我想了很久也不知道怎么能变成上面想要的。希望得到大家的帮助啊。
同时提前祝大家节日快乐啊。import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;public class Test {
public static final String zipFileName = "c:/test.zip";
public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
Person p = new Person("张三", 25);
FileOutputStream fileOutStream = new FileOutputStream("zhangsan");
ObjectOutputStream objectOutStream = new ObjectOutputStream(
fileOutStream);
objectOutStream.writeObject(p);
objectOutStream.close();
p = null; FileInputStream fileInStream = new FileInputStream("zhangsan");
ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(
zipFileName));
zipOutput.putNextEntry(new ZipEntry("zhangsan"));
// 将对象变成文件然后压缩成zip
addFile(zipOutput, fileInStream);
zipOutput.close();
// 将zip解压还原出对象
unzip(new File(zipFileName));
System.out.println("end"); } /**
* 释放文件
*/
private static void extractFile(ZipInputStream zipIns, ZipEntry zipEntry)
throws IOException, ClassNotFoundException {
FileOutputStream ous = new FileOutputStream("tmp");
byte[] tmp = new byte[1024];
int len = 0;
while ((len = zipIns.read(tmp)) != -1)
ous.write(tmp, 0, len);
ous.close();
zipIns.closeEntry();
FileInputStream fileInStream = new FileInputStream("tmp");
ObjectInputStream objectInStream = new ObjectInputStream(fileInStream);
Person p = (Person) objectInStream.readObject();
} /**
* 解压缩
*/
public static void unzip(File input) throws IOException,
ClassNotFoundException { ZipInputStream zipIns = new ZipInputStream(new FileInputStream(input));
ZipEntry zipEntry = zipIns.getNextEntry(); while (zipEntry != null) {
extractFile(zipIns, zipEntry); zipEntry = zipIns.getNextEntry();
}
zipIns.close();
}
/**
* 压缩文件
*/
private static void addFile(ZipOutputStream zipOutput, FileInputStream ins)
throws IOException {
byte[] tmp = new byte[1024];
int len = 0;
while ((len = ins.read(tmp)) != -1)
zipOutput.write(tmp, 0, len);
ins.close();
}}class Person implements Serializable { private String name;
private int age; public Person(String name, int age) {
this.name = name;
this.age = age;
} public int getAge() {
return age;
} public String getName() {
return name;
}
}
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();
}}
555555555555555555555
555555555555555555555