导航:首页 > 编程语言 > javacompress

javacompress

发布时间:2025-09-13 09:36:54

‘壹’ java中将一个文件夹下所有的文件压缩成一个文件,然后,解压到指定目录.

import java.io.*;
import java.util.zip.*;
public class CompressD {
// 缓冲
static byte[] buffer = new byte[2048];
public static void main(String[] args) throws Exception {
// 来源
File inputDir = new File("C:\\CompressTest\\");
// 目标
FileOutputStream fos = new FileOutputStream("C:\\CompressTest.zip");
// 过滤
ZipOutputStream zos = new ZipOutputStream(fos);
// 压缩
zip(inputDir.listFiles(), "", zos);
// 关闭
zos.close();
}
private static void zip(File[] files, String baseFolder, ZipOutputStream zos)
throws Exception {
// 输入
FileInputStream fis = null;
// 条目
ZipEntry entry = null;
// 数目
int count = 0;
for (File file : files) {
if (file.isDirectory()) {
// 递归
zip(file.listFiles(), file.getName() + File.separator, zos);
continue;
}
entry = new ZipEntry(baseFolder + file.getName());
// 加入
zos.putNextEntry(entry);
fis = new FileInputStream(file);
// 读取
while ((count = fis.read(buffer, 0, buffer.length)) != -1)
// 写入
zos.write(buffer, 0, count);
}
}
}

‘贰’ 电脑文件zip如何压缩java实现对zip文件的压缩

电脑文件zip如何压缩(java实现对zip文件的压缩)
一、java实现压缩为zip
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
publicclassZipUtils{
privatestaticfinalintBUFFER_SIZE=2*1024;
publicstaticvoiddirFile(Filedir){
File[]files=dir.listFiles();//得到File数组,获得目录下所有文件
for(Filefile:files){//遍历所有的子目录和文件
if(file.isDirectory()){
dirFile(file);//如果是目录递归调用dirFile()
}
//成功压缩文件后,对原文件进行删除
file.delete();
}
//顺带把对应的目录进行删除
dir.delete();
}
/**
*压缩成ZIP方法1
*@paramsrcDir压缩文件夹路径
*@paramout压缩文件输出流
*@paramKeepDirstructure是否保留原来的目录结构,true:保留目录结构;
*false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
*@throwsRuntimeException压缩失败会抛出运行时异常
*/
publicstaticvoidtoZip(StringsrcDir,OutputStreamout,booleanKeepDirstructure)
throwsRuntimeException{
longstart=System.currentTimeMillis();
ZipOutputStreamzos=null;
try{
zos=newZipOutputStream(out);
FilesourceFile=newFile(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirstructure);
longend=System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"ms");
}catch(Exceptione){
thrownewRuntimeException("ziperrorfromZipUtils",e);
}finally{
if(zos!=null){
try{
zos.close();
}catch(IOExceptione){
e.printstacktrace();
}
}
}
}
/**
*递归压缩方法
*@paramsourceFile源文件
*@paramzoszip输出流
*@paramname压缩后的名称
*@paramKeepDirstructure是否保留原来的目录结构,会压缩失败)
*@throwsException
*/
privatestaticvoidcompress(FilesourceFile,ZipOutputStreamzos,Stringname,
booleanKeepDirstructure)throwsException{
byte[]buf=newbyte[BUFFER_SIZE];
if(sourceFile.isFile()){
//向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(newZipEntry(name));
//文件到zip输出流中
intlen;
FileInputStreamin=newFileInputStream(sourceFile);
while((len=in.read(buf))!=-1){
zos.write(buf,len);
}
//Completetheentry
zos.closeEntry();
in.close();
}else{
File[]listFiles=sourceFile.listFiles();
if(listFiles==null||listFiles.length==0){
//需要保留原来的文件结构时,需要对空文件夹进行处理
if(KeepDirstructure){
//空文件夹的处理
zos.putNextEntry(newZipEntry(name+"/"));
//没有文件,不需要文件的
zos.closeEntry();
}
}else{
for(Filefile:listFiles){
//判断是否需要保留原来的文件结构
if(KeepDirstructure){
//注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
//不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file,name+"/"+file.getName(),KeepDirstructure);
}else{
compress(file,file.getName(),KeepDirstructure);
}
}
}
}
}
}

‘叁’ Java中如何将tar归档文件压缩成tar.Z文件

zcat命令

compress [-fv] [-b bits] [file...]

compress [-cfv] [-b bits] [file]

uncompress [-cfv] [file...]

zcat [file...]

Process p = Runtime.getRuntime().exec("ps -ef");

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));

// read the output from the command

System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// read any errors from the attempted command

System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

System.exit(0);

阅读全文

与javacompress相关的资料

热点内容
multikey写入加密狗 浏览:399
网上银行服务器反馈地址 浏览:478
酷狗音乐存放音乐的文件夹 浏览:597
文件夹支架图片 浏览:898
毛笔app哪个好 浏览:464
程序员在厂里打工怎么样 浏览:807
泰安联想加密u盘哪里买 浏览:613
有什么单机听音乐的app 浏览:577
oppor11服务器地址 浏览:187
传统版布林线源码 浏览:745
app黑色是什么原因 浏览:618
王者荣耀如何说自己是程序员 浏览:734
安川7源码 浏览:600
用什么软件修改app数据 浏览:571
app里面都有什么 浏览:516
服务器8h是什么 浏览:1000
源码修改对应的位置 浏览:123
赛车游戏源码价值几千 浏览:404
单片机rom只读吗 浏览:153
android服务器时间同步 浏览:34