当前位置:首页 » 文件管理 » java把文件夹打包成zip文件

java把文件夹打包成zip文件

发布时间: 2022-10-02 08:12:22

A. 如何用java把内存里的二进制文件打包成ZIP包

Java代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

class ZipTest {
// 压缩
public static void zip(String zipFileName, String inputFile)
throws Exception {
File f = new File(inputFile);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, f, f.getName());
System.out.println("zip done");
out.close();
}

private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}

public static void main(String[] args) {
try {
ZipTest t = new ZipTest();
t.zip("c:\\test.zip", "c:\\1.txt");
} catch (Exception e) {
e.printStackTrace(System.out);
}

}
}

B. java怎么将文件打包成zip包,并且源文件还在,只是多了个zip包,网上多是直接压缩的,导致源文件不在了

选择这个添加到压缩文件,就是保留之前的,多出一份压缩的啊

C. 谁有把多个文件夹压缩成zip文件的java方法分享一个

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("d:\\test.zip"));
String test1="test1";
String test2="test2";
byte[] bytes1 = test1.getBytes("UTF-8");
byte[] bytes2 = test2.getBytes("UTF-8");

ZipEntry z1 = new ZipEntry("test1.txt");
zos.putNextEntry(z1);
zos.write(bytes1);
ZipEntry z2 = new ZipEntry("text2.txt");
zos.putNextEntry(z2);
zos.write(bytes2);
zos.closeEntry();
zos.close();

//流可以自己获取

//java默认的包不支持中文(乱码)

//使用apache的ZipOutputStream进行zip压缩

D. java 如何将多个文件打包成一个zip

java 将多个文件打包成一个zip的代码如下:
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
for(int i=0;i<fileList.size();i++){
String filename = (String)fileList.get(i);
File file = new File(filename);
zip(out,file);
}
out.close();

E. java编写的代码怎么压缩成zip文件

摘要 (1)可以压缩文件,也可以压缩文件夹

F. java 如何将多个文件打包成一个zip后进行下载

打包压缩的如下:
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
for(int i=0;i<fileList.size();i++){
String filename = (String)fileList.get(i);
File file = new File(filename);
zip(out,file);
}
out.close();

下载的如下:
private int blockSize=65000;
File file = new File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
int readBytes = 0;
readBytes = fileIn.read(b, 0, blockSize);
totalRead += readBytes;
out.write(b, 0, readBytes);

代码大致如此,请参考。

G. 如何用JAVA把内存里的二进制文件打包成ZIP包

在JDK中有一个zip工具类:<blockquote>java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.</blockquote>使用此类可以将文件夹或者多个文件进行打包压缩操作。在使用之前先了解关键方法:<blockquote>ZipEntry(String name) Creates a new zip entry with the specified name.</blockquote>使用ZipEntry的构造方法可以创建一个zip压缩文件包的实例,然后通过ZipOutputStream将待压缩的文件以流的形式写进该压缩包中。具体实现代码如下:<pre t="code" l="java">import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 将文件夹下面的文件
* 打包成zip压缩文件
*
* @author admin
*
*/
public final class FileToZip {

private FileToZip(){}

/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;

if(sourceFile.exists() == false){
System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");
}else{
try {
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");
}else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}

public static void main(String[] args){
String sourceFilePath = "D:\\TestFile";
String zipFilePath = "D:\\tmp";
String fileName = "file";
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失败!");
}
}

}

H. 如何使用java压缩文件夹成为zip包(最简单的

import java.io.File;

public class ZipCompressorByAnt {

private File zipFile;

/**
* 压缩文件构造函数
* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile);
}

/**
* 执行压缩操作
* @param srcPathName 需要被压缩的文件/文件夹
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName);

File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}

Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); //排除哪些文件或文件夹
zip.addFileset(fileSet);
zip.execute();
}

}

public class TestZip {

public static void main(String[] args) {

ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\test1.zip ");
zca.compressExe("E:\test1");
}

}

/*如果 出现ant 的 52 51 50 等版本问题 可以去找对应的ant-1.8.2.jar 我开始用的ant-1.10.1.jar 就是这个包版本高了 一直报verson 52 版本问题*/

I. 如何用java 将文件加密压缩为zip文件.

用java加密压缩zip文件:
package com.ninemax.demo.zip.decrypt;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;

import org.apache.commons.io.FileUtils;

import de.idyl.winzipaes.AesZipFileDecrypter;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESDecrypter;
import de.idyl.winzipaes.impl.AESDecrypterBC;
import de.idyl.winzipaes.impl.AESEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import de.idyl.winzipaes.impl.ExtZipEntry;

/**
* 压缩指定文件或目录为ZIP格式压缩文件
* 支持中文(修改源码后)
* 支持密码(仅支持256bit的AES加密解密)
* 依赖bcprov项目(bcprov-jdk16-140.jar)
*
* @author zyh
*/
public class DecryptionZipUtil {

/**
* 使用指定密码将给定文件或文件夹压缩成指定的输出ZIP文件
* @param srcFile 需要压缩的文件或文件夹
* @param destPath 输出路径
* @param passwd 压缩文件使用的密码
*/
public static void zip(String srcFile,String destPath,String passwd) {
AESEncrypter encrypter = new AESEncrypterBC();
AesZipFileEncrypter zipFileEncrypter = null;
try {
zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);
/**
* 此方法是修改源码后添加,用以支持中文文件名
*/
zipFileEncrypter.setEncoding("utf8");
File sFile = new File(srcFile);
/**
* AesZipFileEncrypter提供了重载的添加Entry的方法,其中:
* add(File f, String passwd)
* 方法是将文件直接添加进压缩文件
*
* add(File f, String pathForEntry, String passwd)
* 方法是按指定路径将文件添加进压缩文件
* pathForEntry - to be used for addition of the file (path within zip file)
*/
doZip(sFile, zipFileEncrypter, "", passwd);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipFileEncrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 具体压缩方法,将给定文件添加进压缩文件中,并处理压缩文件中的路径
* @param file 给定磁盘文件(是文件直接添加,是目录递归调用添加)
* @param encrypter AesZipFileEncrypter实例,用于输出加密ZIP文件
* @param pathForEntry ZIP文件中的路径
* @param passwd 压缩密码
* @throws IOException
*/
private static void doZip(File file, AesZipFileEncrypter encrypter,
String pathForEntry, String passwd) throws IOException {
if (file.isFile()) {
pathForEntry += file.getName();
encrypter.add(file, pathForEntry, passwd);
return;
}
pathForEntry += file.getName() + File.separator;
for(File subFile : file.listFiles()) {
doZip(subFile, encrypter, pathForEntry, passwd);
}
}

/**
* 使用给定密码解压指定压缩文件到指定目录
* @param inFile 指定Zip文件
* @param outDir 解压目录
* @param passwd 解压密码
*/
public static void unzip(String inFile, String outDir, String passwd) {
File outDirectory = new File(outDir);
if (!outDirectory.exists()) {
outDirectory.mkdir();
}
AESDecrypter decrypter = new AESDecrypterBC();
AesZipFileDecrypter zipDecrypter = null;
try {
zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);
AesZipFileDecrypter.charset = "utf-8";
/**
* 得到ZIP文件中所有Entry,但此处好像与JDK里不同,目录不视为Entry
* 需要创建文件夹,entry.isDirectory()方法同样不适用,不知道是不是自己使用错误
* 处理文件夹问题处理可能不太好
*/
List<ExtZipEntry> entryList = zipDecrypter.getEntryList();
for(ExtZipEntry entry : entryList) {
String eName = entry.getName();
String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);
File extractDir = new File(outDir, dir);
if (!extractDir.exists()) {
FileUtils.forceMkdir(extractDir);
}
/**
* 抽出文件
*/
File extractFile = new File(outDir + File.separator + eName);
zipDecrypter.extractEntry(entry, extractFile, passwd);
}
} catch (IOException e) {
e.printStackTrace();
} catch (DataFormatException e) {
e.printStackTrace();
} finally {
try {
zipDecrypter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 测试
* @param args
*/
public static void main(String[] args) {
/**
* 压缩测试
* 可以传文件或者目录
*/
// zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");
// zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");

unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");
}
}
压缩多个文件时,有两个方法(第一种没试):
(1) 预先把多个文件压缩成zip,然后调用enc.addAll(inZipFile, password);方法将多个zip文件加进来。
(2)针对需要压缩的文件循环调用enc.add(inFile, password);,每次都用相同的密码。

J. 怎样用Java生成ZIP文件

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;


import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;


/**

* @project: Test

* @author chenssy

* @date 2013-7-28

* @Description: 文件压缩工具类

* 将指定文件/文件夹压缩成zip、rar压缩文件

*/

public class CompressedFileUtil {

/**

* 默认构造函数

*/

public CompressedFileUtil(){

}


/**

* @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip

* @param resourePath 源文件/文件夹

* @param targetPath 目的压缩文件保存路径

* @return void

* @throws Exception

*/

public void compressedFile(String resourcesPath,String targetPath) throws Exception{

File resourcesFile = new File(resourcesPath); //源文件

File targetFile = new File(targetPath); //目的

//如果目的路径不存在,则新建

if(!targetFile.exists()){

targetFile.mkdirs();

}

String targetName = resourcesFile.getName()+".zip"; //目的压缩文件名

FileOutputStream outputStream = new FileOutputStream(targetPath+"\"+targetName);

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));

createCompressedFile(out, resourcesFile, "");

out.close();

}

/**

* @desc 生成压缩文件。

* 如果是文件夹,则使用递归,进行文件遍历、压缩

* 如果是文件,直接压缩

* @param out 输出流

* @param file 目标文件

* @return void

* @throws Exception

*/

public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{

//如果当前的是文件夹,则进行进一步处理

if(file.isDirectory()){

//得到文件列表信息

File[] files = file.listFiles();

//将文件夹添加到下一级打包目录

out.putNextEntry(new ZipEntry(dir+"/"));

dir = dir.length() == 0 ? "" : dir +"/";

//循环将文件夹中的文件打包

for(int i = 0 ; i < files.length ; i++){

createCompressedFile(out, files[i], dir + files[i].getName()); //递归处理

}

}

else{ //当前的是文件,打包处理

//文件输入流

FileInputStream fis = new FileInputStream(file);

out.putNextEntry(new ZipEntry(dir));

//进行写操作

int j = 0;

byte[] buffer = new byte[1024];

while((j = fis.read(buffer)) > 0){

out.write(buffer,0,j);

}

//关闭输入流

fis.close();

}

}

public static void main(String[] args){

CompressedFileUtil compressedFileUtil = new CompressedFileUtil();

try {

compressedFileUtil.compressedFile("G:\zip", "F:\zip");

System.out.println("压缩文件已经生成...");

} catch (Exception e) {

System.out.println("压缩文件生成失败...");

e.printStackTrace();

}

}

}

运行程序结果如下:

如果是使用java.util下的java.util.zip进行打包处理,可能会出现中文乱码问题,这是因为java的zip方法不支持编码格式的更改,我们可以使用ant.java下的zip工具类来进行打包处理。所以需要将ant.jar导入项目的lib目录下。

热点内容
pythontkinter大小 发布:2024-04-27 14:51:22 浏览:114
pc端好用的c语言编译器 发布:2024-04-27 14:50:22 浏览:502
爬虫脚本如何运行在服务器 发布:2024-04-27 14:50:22 浏览:1
dropzone上传 发布:2024-04-27 14:39:31 浏览:880
ins安卓版快拍为什么没有特效 发布:2024-04-27 14:33:41 浏览:592
cs服务器ip在哪里 发布:2024-04-27 14:25:58 浏览:37
华为安卓怎么上脸书 发布:2024-04-27 14:24:20 浏览:841
我的世界手机版服务器冷知识 发布:2024-04-27 14:11:10 浏览:790
文件横向加密 发布:2024-04-27 14:06:38 浏览:497
python列表推导 发布:2024-04-27 14:01:46 浏览:357