查了好长时间,java中好像GZ只可以压缩单个文件。
根据网上的资料,用tar过度,写了一个完整的压缩和解压。
只是功能的实现,压缩的文件夹下面不能有文件夹,只能有文件,传入的目录必须存在,最后解压的目录也必须存在。
要导入一个包 ant.jar,我上传了,在附件里
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.apache.tools.tar.TarOutputStream;
public class GZyasuojieya {
private static int BUFFER = 1024 * 4;
private static byte[] B_ARRAY = new byte[BUFFER];
/**
* file变为tar文件
*/
private static void file2tar(String filesPath, String tarPath) {
File fileDirectory = new File(filesPath);
int length = fileDirectory.listFiles().length;
File[] files = fileDirectory.listFiles();
try {
File tarFile = new File(tarPath);
tarFile.createNewFile();
FileOutputStream fout = new FileOutputStream(tarFile);
TarOutputStream tout = new TarOutputStream(fout);
for (int i = 0; i < length; i++) {
String filename = fileDirectory.getPath() + File.separator
+ files[i].getName();
FileInputStream in = new FileInputStream(filename);
TarEntry tarEn = new TarEntry(files[i]);
tarEn.setName(files[i].getName());
tout.putNextEntry(tarEn);
int num;
while ((num = in.read(B_ARRAY)) != -1) {
tout.write(B_ARRAY, 0, num);
}
tout.closeEntry();
in.close();
}
tout.close();
fout.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
/**
* 传入tar的文件路径,产生GZ包
*
* @return
*/
public static void tar2gz(String tarPath, String gzPath) {
File srcFile = new File(tarPath);
File targetFile = new File(gzPath);
try {
FileInputStream in = null;
GZIPOutputStream out = null;
in = new FileInputStream(srcFile);
out = new GZIPOutputStream(new FileOutputStream(targetFile));
int number = 0;
while ((number = in.read(B_ARRAY, 0, BUFFER)) != -1) {
out.write(B_ARRAY, 0, number);
}
in.close();
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
/**
* gz包变为tar文件
*/
public static void gz2tar(String gzPath, String tarPath) {
try {
GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(
gzPath));
OutputStream out = new FileOutputStream(tarPath);
int number = 0;
while ((number = gzin.read(B_ARRAY, 0, BUFFER)) != -1) {
out.write(B_ARRAY, 0, number);
}
gzin.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* tar 文件变为files
*/
public static void tar2files(String tarPath, String filesPath) {
try {
FileOutputStream out = null;
TarInputStream in = new TarInputStream(new FileInputStream(tarPath));
TarEntry entry = null;
File outFile = null;
while ((entry = in.getNextEntry()) != null) {
outFile = new File(filesPath + entry.getName());
outFile.createNewFile();
out = new FileOutputStream((outFile));
int number;
while ((number = in.read(B_ARRAY, 0, BUFFER)) != -1) {
out.write(B_ARRAY, 0, number);
}
// while (true) {
// int readsize = in.read(B_ARRAY);
// out.write(B_ARRAY);
// if (readsize < BUFFER) {
// break;
// }
// }
}
out.close();
in.close();
} catch (Exception e) {
}
}
public static void main(String args[]) {
String filesPath = "D:\\123";
String tarPath = "D:\\123.tar";
String gzPath = "D:\\123.tar.gz";
file2tar(filesPath, tarPath);
tar2gz(tarPath,gzPath);
gz2tar(gzPath, tarPath);
tar2files(tarPath, "D:\\123\\");
}
}