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();
下載的如下:
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);
代碼大致如此,請參考。
❷ 如何使用java壓縮文件夾成為zip包
在JDK中有一個zip工具類:
java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.
使用此類可以將文件夾或者多個文件進行打包壓縮操作。
在使用之前先了解關鍵方法:
ZipEntry(String name) Creates a new zip entry with the specified name.
使用ZipEntry的構造方法可以創建一個zip壓縮文件包的實例,然後通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
/**
*將文件夾下面的文件
*打包成zip壓縮文件
*
*@authoradmin
*
*/
publicfinalclassFileToZip{
privateFileToZip(){}
/**
*將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
*@paramsourceFilePath:待壓縮的文件路徑
*@paramzipFilePath:壓縮後存放路徑
*@paramfileName:壓縮後文件的名稱
*@return
*/
publicstaticbooleanfileToZip(StringsourceFilePath,StringzipFilePath,StringfileName){
booleanflag=false;
FilesourceFile=newFile(sourceFilePath);
FileInputStreamfis=null;
BufferedInputStreambis=null;
FileOutputStreamfos=null;
ZipOutputStreamzos=null;
if(sourceFile.exists()==false){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try{
FilezipFile=newFile(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=newFileOutputStream(zipFile);
zos=newZipOutputStream(newBufferedOutputStream(fos));
byte[]bufs=newbyte[1024*10];
for(inti=0;i<sourceFiles.length;i++){
//創建ZIP實體,並添加進壓縮包
ZipEntryzipEntry=newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件並寫進壓縮包里
fis=newFileInputStream(sourceFiles[i]);
bis=newBufferedInputStream(fis,1024*10);
intread=0;
while((read=bis.read(bufs,0,1024*10))!=-1){
zos.write(bufs,0,read);
}
}
flag=true;
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
//關閉流
try{
if(null!=bis)bis.close();
if(null!=zos)zos.close();
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
returnflag;
}
publicstaticvoidmain(String[]args){
StringsourceFilePath="D:\TestFile";
StringzipFilePath="D:\tmp";
StringfileName="12700153file";
booleanflag=FileToZip.fileToZip(sourceFilePath,zipFilePath,fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
}
}
❸ 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();
❹ java打zip包
你可以通過Java內置的打包功能完成啊,單擊右鍵可以選擇Export裡面有很多的打包類型可以供你選擇
❺ java 如何將 txt 文件 變成zip壓縮文件 求例子!!
這個要用 壓縮流類 ZipOutputStream
下面是一個例子 在D盤下有個 名字叫 demo.txt的文件.程序運行後會再D盤下生成一個demo.zip的文件,以下是代碼:
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputStreamDemo {
public static void main(String args[]) throws IOException {
//定義要壓縮的文件 也就是說在D盤里有個 demo.txt 的文件(必須要有,否者會有異常,實際應用中可判斷);
File file = new File("d:" + File.separator + "demo.txt");
//定義壓縮文件的名稱
File zipFile = new File("d:" + File.separator + "demo.zip");
//定義輸入文件流
InputStream input = new FileInputStream(file);
//定義壓縮輸出流
ZipOutputStream zipOut = null;
//實例化壓縮輸出流,並制定壓縮文件的輸出路徑 就是D盤下,名字叫 demo.zip
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
//設置注釋
zipOut.setComment("www.demo.com");
int temp = 0;
while((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
zipOut.close();
}
}
希望能幫助樓主,建議樓主多看看JDK文檔,設計到文件的輸出什麼都在JAVA.IO包里,好好看看..
不過樓主要知道,壓縮流也是inputstream和outputstream的子類,但是並沒有定義在java.io包里,而是以一個工具類的形式出現的,但是在用的時候還是需要java.io包的支持的...
❻ 如何用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("文件打包失敗!");
}
}
}
❼ 怎樣用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目錄下。
❽ java程序 壓縮指定文件 格式(zip)
直接右鍵單擊文件夾(所有的文件存放在一個文件夾裡面)然後選擇壓縮 前提是你要有 zip壓縮程序
❾ java編寫的代碼怎麼壓縮成zip文件
摘要 (1)可以壓縮文件,也可以壓縮文件夾
❿ 如何使用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 版本問題*/