當前位置:首頁 » 編程軟體 » 怎麼用腳本打包成zip

怎麼用腳本打包成zip

發布時間: 2023-08-13 00:40:59

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();

⑷ bat腳本,實現壓縮當前文件夾下的第二層文件夾分別為zip壓縮包

不清楚你的實際文件/情況,僅以問題中的樣例/說明為據;以下代碼復制粘貼到記事本,另存為xx.bat,編碼選ANSI
@echo off
rem 將當前目錄下指定層級的子文件夾進行壓縮打包
set #=Any question&set _=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%_% %z%
cd /d "%~dp0"
set "exefile=C:\Program Files\WinRAR\WinRAR.exe"
if not exist "%exefile%" (echo;"%exefile%" 未找到&pause&exit)
for /f "delims=" %%a in ('dir /ad/b') do (
for /f "delims=" %%b in ('dir /ad/b ".\%%a\" 2^>nul') do (
"%exefile%" a -y -r -ep1 ".\%%~nxa\%%~nb.zip" ".\%%~nxa\%%~nxb\"
)
)
echo;%#% +%$%%$%/%_% %z%
pause
exit

⑸ 如何用bat命令把當前目錄的所有文件和文件夾打包為一個zip

@echooff&title打包當前目錄By依夢琴瑤

setWinRAR_Dir=%ProgramFiles%WinRAR

setPATH=%PATH%;%WinRAR_Dir%

WinRAR.exea-r"當前目錄備份.zip"-rr-m5*-ibck
pause

熱點內容
安卓哪個國家免費用 發布:2025-02-04 22:39:54 瀏覽:59
電腦配置低但想玩小偷模擬器怎麼辦 發布:2025-02-04 22:39:03 瀏覽:233
最快腳本語言 發布:2025-02-04 22:27:23 瀏覽:527
安卓的人臉識別在哪裡 發布:2025-02-04 22:16:45 瀏覽:674
悠然伺服器的ip是什麼 發布:2025-02-04 22:10:17 瀏覽:65
3des源碼 發布:2025-02-04 22:09:16 瀏覽:809
如何備份資料庫表 發布:2025-02-04 22:09:07 瀏覽:294
如何刪除下載的鬧鍾鈴聲安卓 發布:2025-02-04 22:03:35 瀏覽:659
死神腳本 發布:2025-02-04 21:57:03 瀏覽:168
phpposthtml 發布:2025-02-04 21:37:46 瀏覽:89