急!!!如何压缩、解压.gz的压缩文件 问题描述:有主目录(maindir),下面有多个子目录,子目录下若干个文件待解决问题:如何将目录压缩成.gz格式的压缩文件,并解压,并按行读取目录下的文件的内容 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主这个用tar命令就可以了阿难道不会想要用java实现吧 A:LINUX下:01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)02-.gz格式解压1:[*******]$ gunzip FileName.gz解压2:[*******]$ gzip -d FileName.gz压 缩:[*******]$ gzip FileNameB:WINDOWS下:Total Commander用这个软件 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;/** * 压缩实现类 <br> * 主要实现: <br> * <p>压缩单个文件、 * <p>压缩文件夹下的所有文件及子文件夹 * */public class ZipTool { /** * 压缩单个文件 * @param filePath 文件路径 * @param fileName 文件名字 * @param objDir 压缩文件目标文件夹 * @param ojbZipName 压缩文件名字 * @return * @throws IOException */ public boolean zip(String filePath, String fileName, String objDir, String ojbZipName) throws IOException { boolean tag = false; ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objDir + ojbZipName)); FileInputStream fis = new FileInputStream(filePath + fileName); byte[] b = new byte[4096]; int i = 0; zos.putNextEntry(new ZipEntry(fileName)); while ((i = (fis.read(b))) > 0) { zos.write(b, 0, i); } fis.close(); zos.close(); return tag; } /** * 压缩一个文件夹下的所有文件 注意中间路径连接用"/" 如:c:/tt/ttt * @param srcPath 要压缩的文件夹路径 * @param objZipPath 目标文件夹路径 * @param ojbZipName 目标压缩文件名字 * @return */ public boolean zipDir(String srcPath, String objZipPath, String ojbZipName){ ZipOutputStream zos = null; try { File objFile = new File(objZipPath , ojbZipName); zos = new ZipOutputStream(new FileOutputStream(objFile)); if (srcPath == null) { System.out.println("传入的源文件夹路径字符串不能为空!"); throw new java.lang.NullPointerException(); } String dirName = ""; File file = new File(srcPath); if (!file.isDirectory()) { throw new Exception("传入了不正确的源文件夹路径!"); } else { dirName = srcPath.substring(srcPath.lastIndexOf("/") + 1); if (dirName == null || "".equals(dirName)) { String subStr = srcPath.substring(0, srcPath.length() - 2); dirName = subStr.substring(subStr.lastIndexOf("/") + 1); } ZipEntry ze = new ZipEntry(dirName + "/"); zos.putNextEntry(ze); if (dirName == null || "".equals(dirName)) { throw new Exception("传入了不正确的源文件夹路径!"); } } File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(dirName + "/", files[i], zos); } return true; } catch (Exception e) { System.out.println("压缩文件时出现异常!"); e.printStackTrace(); return false; }finally{ if(zos != null){ try { zos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } /** * 用来压缩文件夹下的所有子文件 * 此方法为一个递归调法方法 * @param zipPath 要压缩的文件在压缩包中的相对路径 * @param file 要压缩的文件引用 * @param zos 压缩文件输出流 * @throws IOException */ private void zipFile(String zipPath, File file, ZipOutputStream zos) throws IOException { //是文件夹的操作 if (file.isDirectory()) { ZipEntry ze = new ZipEntry(zipPath + file.getName() + "/"); zos.putNextEntry(ze); //递归调用 for (int i = 0; i < file.listFiles().length; i++) { zipFile(zipPath + file.getName() + "/", file.listFiles()[i], zos); } //是文件时的操作 }else{ FileInputStream fis = null; try{ fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(zipPath + file.getName()); zos.putNextEntry(ze); byte []b = new byte[4096]; int i = 0; while( ( i = (fis.read(b)) ) > 0 ){ zos.write(b, 0, i); } }finally{ if(fis != null){ fis.close(); } } } } public static void main(String[] args) { ZipTool zt = new ZipTool(); try { zt.zipDir("c:/gif", "c:/", "test.zip"); } catch (Exception e) { e.printStackTrace(); } }} windows下,还真没碰到过.gz的文件。 我想用java进行压缩、解压,不是用Linux命令 Runtime.exce调用脚本进行压缩和解压吧! 问个白痴问题?.gz是用什么压缩流压缩!gzip吗?? 在处理压缩文件时,java使用ZipInputStream读入ZIP文件。要写出ZIP文件,可以使用ZipOutputStream。 4楼的方法在linux下能行的通吗? 根据猫扑同名漫画写的《自助投胎机》,欢迎下载自助投胎 读文件求救?????? 我把javamail API下载了下来,然后怎么安装它? 【Java初学者问道】请详解Java体系! 中央空调控制系统 关于java调用exe如何取得返回值? java记事本程序 我的程序为什么需要Tomcat重启一次后再能正常运行?(jsp+javaBeans+Oracle) 我的java applet怎么不能显示?? 请问哪有corejava2配套光盘的源代码的下载?能下载立刻给分! ==正则问题,谢谢。急= 十分急!几个笔试题,帮忙看看!!!!
难道不会想要用java实现吧
解包:[*******]$ tar xvf FileName.tar
打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)02-.gz格式
解压1:[*******]$ gunzip FileName.gz
解压2:[*******]$ gzip -d FileName.gz
压 缩:[*******]$ gzip FileName
B:WINDOWS下:
Total Commander用这个软件
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;/**
* 压缩实现类 <br>
* 主要实现: <br>
* <p>压缩单个文件、
* <p>压缩文件夹下的所有文件及子文件夹
*
*/public class ZipTool { /**
* 压缩单个文件
* @param filePath 文件路径
* @param fileName 文件名字
* @param objDir 压缩文件目标文件夹
* @param ojbZipName 压缩文件名字
* @return
* @throws IOException
*/
public boolean zip(String filePath, String fileName, String objDir,
String ojbZipName) throws IOException {
boolean tag = false;
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objDir
+ ojbZipName)); FileInputStream fis = new FileInputStream(filePath + fileName);
byte[] b = new byte[4096];
int i = 0; zos.putNextEntry(new ZipEntry(fileName));
while ((i = (fis.read(b))) > 0) {
zos.write(b, 0, i);
} fis.close();
zos.close(); return tag;
}
/**
* 压缩一个文件夹下的所有文件 注意中间路径连接用"/" 如:c:/tt/ttt
* @param srcPath 要压缩的文件夹路径
* @param objZipPath 目标文件夹路径
* @param ojbZipName 目标压缩文件名字
* @return
*/
public boolean zipDir(String srcPath, String objZipPath, String ojbZipName){ ZipOutputStream zos = null;
try {
File objFile = new File(objZipPath , ojbZipName);
zos = new ZipOutputStream(new FileOutputStream(objFile)); if (srcPath == null) {
System.out.println("传入的源文件夹路径字符串不能为空!");
throw new java.lang.NullPointerException();
}
String dirName = "";
File file = new File(srcPath);
if (!file.isDirectory()) {
throw new Exception("传入了不正确的源文件夹路径!");
} else {
dirName = srcPath.substring(srcPath.lastIndexOf("/") + 1);
if (dirName == null || "".equals(dirName)) {
String subStr = srcPath.substring(0, srcPath.length() - 2);
dirName = subStr.substring(subStr.lastIndexOf("/") + 1);
}
ZipEntry ze = new ZipEntry(dirName + "/");
zos.putNextEntry(ze);
if (dirName == null || "".equals(dirName)) {
throw new Exception("传入了不正确的源文件夹路径!");
}
} File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) {
zipFile(dirName + "/", files[i], zos);
}
return true;
} catch (Exception e) {
System.out.println("压缩文件时出现异常!");
e.printStackTrace();
return false;
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} } /**
* 用来压缩文件夹下的所有子文件
* 此方法为一个递归调法方法
* @param zipPath 要压缩的文件在压缩包中的相对路径
* @param file 要压缩的文件引用
* @param zos 压缩文件输出流
* @throws IOException
*/
private void zipFile(String zipPath, File file, ZipOutputStream zos)
throws IOException {
//是文件夹的操作
if (file.isDirectory()) {
ZipEntry ze = new ZipEntry(zipPath + file.getName() + "/");
zos.putNextEntry(ze);
//递归调用
for (int i = 0; i < file.listFiles().length; i++) {
zipFile(zipPath + file.getName() + "/", file.listFiles()[i], zos);
}
//是文件时的操作
}else{
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
ZipEntry ze = new ZipEntry(zipPath + file.getName());
zos.putNextEntry(ze);
byte []b = new byte[4096];
int i = 0;
while( ( i = (fis.read(b)) ) > 0 ){
zos.write(b, 0, i);
}
}finally{
if(fis != null){
fis.close();
}
}
}
} public static void main(String[] args) {
ZipTool zt = new ZipTool();
try {
zt.zipDir("c:/gif", "c:/", "test.zip");
} catch (Exception e) {
e.printStackTrace();
}
}}
调用脚本进行压缩和解压吧!