tar
-c: 建立壓縮檔案
-x:解壓
-t:查看內容
-r:向壓縮歸檔文件末尾追加文件
-u:更新原壓縮包中的文件
這五個是獨立的命令,壓縮解壓都要用到其中一個,可以和別的命令連用但只能用其中一個。下面的參數是根據需要在壓縮或解壓檔案時可選的。
-z:有gzip屬性的
-j:有bz2屬性的
-Z:有compress屬性的
-v:顯示所有過程
-O:將文件解開到標准輸出
下面的參數-f是必須的
-f: 使用檔案名字,切記,這個參數是最後一個參數,後面只能接檔案名。
# tar -cf all.tar *.jpg
這條命令是將所有.jpg的文件打成一個名為all.tar的包。-c是表示產生新的包,-f指定包的文件名。
# tar -rf all.tar *.gif
這條命令是將所有.gif的文件增加到all.tar的包裡面去。-r是表示增加文件的意思。
# tar -uf all.tar logo.gif
這條命令是更新原來tar包all.tar中logo.gif文件,-u是表示更新文件的意思。
# tar -tf all.tar
這條命令是列出all.tar包中所有文件,-t是列出文件的意思
# tar -xf all.tar
這條命令是解出all.tar包中所有文件,-t是解開的意思
壓縮
tar -cvf jpg.tar *.jpg //將目錄里所有jpg文件打包成tar.jpg
tar -czf jpg.tar.gz *.jpg //將目錄里所有jpg文件打包成jpg.tar後,並且將其用gzip壓縮,生成一個gzip壓縮過的包,命名為jpg.tar.gz
tar -cjf jpg.tar.bz2 *.jpg //將目錄里所有jpg文件打包成jpg.tar後,並且將其用bzip2壓縮,生成一個bzip2壓縮過的包,命名為jpg.tar.bz2
tar -cZf jpg.tar.Z *.jpg //將目錄里所有jpg文件打包成jpg.tar後,並且將其用compress壓縮,生成一個umcompress壓縮過的包,命名為jpg.tar.Z
rar a jpg.rar *.jpg //rar格式的壓縮,需要先下載rar for linux
zip jpg.zip *.jpg //zip格式的壓縮,需要先下載zip for linux
解壓
tar -xvf file.tar //解壓 tar包
tar -xzvf file.tar.gz //解壓tar.gz
tar -xjvf file.tar.bz2 //解壓 tar.bz2
tar -xZvf file.tar.Z //解壓tar.Z
unrar e file.rar //解壓rar
unzip file.zip //解壓zip
總結
1、*.tar 用 tar -xvf 解壓
2、*.gz 用 gzip -d或者gunzip 解壓
3、*.tar.gz和*.tgz 用 tar -xzf 解壓
4、*.bz2 用 bzip2 -d或者用bunzip2 解壓
5、*.tar.bz2用tar -xjf 解壓
6、*.Z 用 uncompress 解壓
7、*.tar.Z 用tar -xZf 解壓
8、*.rar 用 unrar e解壓
9、*.zip 用 unzip 解壓
解壓jdk到指定文件夾:
tar -xzvf jdk-8u131-linux-x64.tar.gz -C /usr/local/java
root@ubuntu:~# tar --help
用法: tar [選項...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore indivial files from the archive.
Examples:
tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.
tar -tvf archive.tar # List all files in archive.tar verbosely.
tar -xf archive.tar # Extract all files from archive.tar.
主操作模式:
-A, --catenate, --concatenate 追加 tar 文件至歸檔
-c, --create 創建一個新歸檔
-d, --diff, --compare 找出歸檔和文件系統的差異
--delete 從歸檔(非磁帶!)中刪除
-r, --append 追加文件至歸檔結尾
-t, --list 列出歸檔內容
--test-label 測試歸檔卷標並退出
-u, --update 僅追加比歸檔中副本更新的文件
-x, --extract, --get 從歸檔中解出文件
操作修飾符:
--check-device 當創建增量歸檔時檢查設備號(默認)
-g, --listed-incremental=FILE 處理新式的 GNU 格式的增量備份
-G, --incremental 處理老式的 GNU 格式的增量備份
--ignore-failed-read
當遇上不可讀文件時不要以非零值退出
--level=NUMBER 所創建的增量列表歸檔的輸出級別
-n, --seek 歸檔可檢索
--no-check-device 當創建增量歸檔時不要檢查設備號
--no-seek 歸檔不可檢索
--occurrence[=NUMBER] 僅處理歸檔中每個文件的第 NUMBER
個事件;僅當與以下子命令 --delete,
--diff, --extract 或是 --list
中的一個聯合使用時,此選項才有效。而且不管文件列表是以命令行形式給出或是通過
-T 選項指定的;NUMBER 值默認為 1
--sparse-version=MAJOR[.MINOR]
設置所用的離散格式版本(隱含
--sparse)
-S, --sparse 高效處理離散文件
重寫控制:
-k, --keep-old-files don't replace existing files when extracting,
treat them as errors
--keep-directory-symlink preserve existing symlinks to directories when
extracting
--keep-newer-files
不要替換比歸檔中副本更新的已存在的文件
--no-overwrite-dir 保留已存在目錄的元數據
--one-top-level[=DIR] create a subdirectory to avoid having loose files
extracted
--overwrite 解壓時重寫存在的文件
--overwrite-dir 解壓時重寫已存在目錄的元數據(默認)
--recursive-unlink 解壓目錄之前先清除目錄層次
--remove-files 在添加文件至歸檔後刪除它們
--skip-old-files don't replace existing files when extracting,
silently skip over them
-U, --unlink-first 在解壓要重寫的文件之前先刪除它們
-W, --verify 在寫入以後嘗試校驗歸檔
選擇輸出流:
--ignore-command-error 忽略子進程的退出代碼
--no-ignore-command-error
將子進程的非零退出代碼認為發生錯誤
-O, --to-stdout 解壓文件至標准輸出
--to-command=COMMAND
將解壓的文件通過管道傳送至另一個程序
操作文件屬性:
--atime-preserve[=METHOD]
在輸出的文件上保留訪問時間,要麼通過在讀取(默認
METHOD=『replace』)後還原時間,要不就不要在第一次(METHOD=『system』)設置時間
--clamp-mtime only set time when the file is more recent than
what was given with --mtime
--delay-directory-restore
直到解壓結束才設置修改時間和所解目錄的許可權
--group=名稱 強制將 NAME
作為所添加的文件的組所有者
--mode=CHANGES 強制將所添加的文件(符號)更改為許可權
CHANGES
--mtime=DATE-OR-FILE 從 DATE-OR-FILE 中為添加的文件設置
mtime
-m, --touch 不要解壓文件的修改時間
--no-delay-directory-restore
取消 --delay-directory-restore 選項的效果
--no-same-owner
將文件解壓為您所有(普通用戶默認此項)
--no-same-permissions
從歸檔中解壓許可權時使用用戶的掩碼位(默認為普通用戶服務)
--numeric-owner 總是以數字代表用戶/組的名稱
--owner=名稱 強制將 NAME
作為所添加的文件的所有者
-p, --preserve-permissions, --same-permissions
解壓文件許可權信息(默認只為超級用戶服務)
--preserve 與 -p 和 -s 一樣
--same-owner
嘗試解壓時保持所有者關系一致(超級用戶默認此項)
-s, --preserve-order, --same-order
member arguments are listed in the same order as
the files in the archive
--sort=ORDER directory sorting order: none (default), name or
inode
Handling of extended file attributes:
--acls Enable the POSIX ACLs support
--no-acls Disable the POSIX ACLs support
--no-selinux Disable the SELinux context support
--no-xattrs Disable extended attributes support
--selinux Enable the SELinux context support
--xattrs Enable extended attributes support
--xattrs-exclude=MASK specify the exclude pattern for xattr keys
--xattrs-include=MASK specify the include pattern for xattr keys
設備選擇和切換:
-f, --file=ARCHIVE 使用歸檔文件或 ARCHIVE 設備
--force-local
即使歸檔文件存在副本還是把它認為是本地歸檔
-F, --info-script=名稱, --new-volume-script=名稱
在每卷磁帶最後運行腳本(隱含 -M)
-L, --tape-length=NUMBER 寫入 NUMBER × 1024 位元組後更換磁帶
-M, --multi-volume 創建/列出/解壓多卷歸檔文件
--rmt-command=COMMAND 使用指定的 rmt COMMAND 代替 rmt
--rsh-command=COMMAND 使用遠程 COMMAND 代替 rsh
--volno-file=FILE 使用/更新 FILE 中的卷數
設備分塊:
-b, --blocking-factor=BLOCKS 每個記錄 BLOCKS x 512 位元組
-B, --read-full-records 讀取時重新分塊(只對 4.2BSD 管道有效)
-i, --ignore-zeros 忽略歸檔中的零位元組塊(即文件結尾)
--record-size=NUMBER 每個記錄的位元組數 NUMBER,乘以 512
選擇歸檔格式:
-H, --format=FORMAT 創建指定格式的歸檔
FORMAT 是以下格式中的一種:
gnu GNU tar 1.13.x 格式
oldgnu GNU 格式 as per tar <= 1.12
pax POSIX 1003.1-2001 (pax) 格式
posix 等同於 pax
ustar POSIX 1003.1-1988 (ustar) 格式
v7 old V7 tar 格式
--old-archive, --portability
等同於 --format=v7
--pax-option=關鍵字[[:]=值][,關鍵字[[:]=值]]...
控制 pax 關鍵字
--posix 等同於 --format=posix
-V, --label=TEXT 創建帶有卷名 TEXT
的歸檔;在列出/解壓時,使用 TEXT
作為卷名的模式串
壓縮選項:
-a, --auto-compress 使用歸檔後綴名來決定壓縮程序
-I, --use-compress-program=PROG
通過 PROG 過濾(必須是能接受 -d
選項的程序)
-j, --bzip2 通過 bzip2 過濾歸檔
-J, --xz 通過 xz 過濾歸檔
--lzip 通過 lzip 過濾歸檔
--lzma 通過 xz 過濾歸檔
--lzop 通過 xz 過濾歸檔
--no-auto-compress 不使用歸檔後綴名來決定壓縮程序
-z, --gzip, --gunzip, --ungzip 通過 gzip 過濾歸檔
-Z, --compress, --uncompress 通過 compress 過濾歸檔
本地文件選擇:
--add-file=FILE 添加指定的 FILE 至歸檔(如果名字以 -
開始會很有用的)
--backup[=CONTROL] 在刪除前備份,選擇 CONTROL 版本
-C, --directory=DIR 改變至目錄 DIR
--exclude=PATTERN 排除以 PATTERN 指定的文件
--exclude-backups 排除備份和鎖文件
--exclude-caches 除標識文件本身外,排除包含
CACHEDIR.TAG 的目錄中的內容
--exclude-caches-all 排除包含 CACHEDIR.TAG 的目錄
--exclude-caches-under 排除包含 CACHEDIR.TAG
的目錄中所有內容
--exclude-ignore=FILE read exclude patterns for each directory from
FILE, if it exists
--exclude-ignore-recursive=FILE
read exclude patterns for each directory and its
subdirectories from FILE, if it exists
--exclude-tag=FILE 除 FILE 自身外,排除包含 FILE
的目錄中的內容
--exclude-tag-all=FILE 排除包含 FILE 的目錄
--exclude-tag-under=FILE 排除包含 FILE 的目錄中的所有內容
--exclude-vcs 排除版本控制系統目錄
--exclude-vcs-ignores read exclude patterns from the VCS ignore files
-h, --dereference
跟蹤符號鏈接;將它們所指向的文件歸檔並輸出
--hard-dereference
跟蹤硬鏈接;將它們所指向的文件歸檔並輸出
-K, --starting-file=MEMBER-NAME
begin at member MEMBER-NAME when reading the
archive
--newer-mtime=DATE 當只有數據改變時比較數據和時間
--no-null 禁用上一次的效果 --null 選項
--no-recursion 避免目錄中的自動降級
--no-unquote do not unquote input file or member names
--null -T 讀取以空終止的名字,-C 禁用
-N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
只保存比 DATE-OR-FILE 更新的文件
--one-file-system 創建歸檔時保存在本地文件系統中
-P, --absolute-names don't strip leading '/'s from file names
--recursion 目錄遞歸(默認)
--suffix=STRING 在刪除前備份,除非被環境變數
SIMPLE_BACKUP_SUFFIX
覆蓋,否則覆蓋常用後綴(『』)
-T, --files-from=FILE 從 FILE
中獲取文件名來解壓或創建文件
--unquote unquote input file or member names (default)
-X, --exclude-from=FILE 排除 FILE 中列出的模式串
文件名變換:
--strip-components=NUMBER 解壓時從文件名中清除 NUMBER
個引導部分
--transform=EXPRESSION, --xform=EXPRESSION
使用 sed 代替 EXPRESSION
來進行文件名變換
文件名匹配選項(同時影響排除和包括模式串):
--anchored 模式串匹配文件名頭部
--ignore-case 忽略大小寫
--no-anchored patterns match after any '/' (default for
exclusion)
--no-ignore-case 匹配大小寫(默認)
--no-wildcards 逐字匹配字元串
--no-wildcards-match-slash wildcards do not match '/'
--wildcards 使用通配符(默認對 exclusion )
--wildcards-match-slash wildcards match '/' (default for exclusion)
提示性輸出:
--checkpoint[=NUMBER] 每隔 NUMBER
個記錄顯示進度信息(默認為 10 個)
--checkpoint-action=ACTION 在每個檢查點上執行 ACTION
--full-time 按文件原本時間格式列印
--index-file=FILE 將詳細輸出發送至 FILE
-l, --check-links
只要不是所有鏈接都被輸出就列印信息
--no-quote-chars=STRING 禁用來自 STRING 的字元引用
--quote-chars=STRING 來自 STRING 的額外的引用字元
--quoting-style=STYLE 設置名稱引用風格;有效的 STYLE
值請參閱以下說明
-R, --block-number 每個信息都顯示歸檔內的塊數
--show-defaults 顯示 tar 默認選項
--show-omitted-dirs
列表或解壓時,列出每個不匹配查找標準的目錄
--show-snapshot-field-ranges
show valid ranges for snapshot-file fields
--show-transformed-names, --show-stored-names
顯示變換後的文件名或歸檔名
--totals[=SIGNAL] 處理歸檔後列印出總位元組數;當此
SIGNAL 被觸發時帶參數 -
列印總位元組數;允許的信號為:
SIGHUP,SIGQUIT,SIGINT,SIGUSR1 和
SIGUSR2;同時也接受不帶 SIG
前綴的信號名稱
--utc 以 UTC 格式列印文件修改時間
-v, --verbose 詳細地列出處理的文件
--warning=KEYWORD 警告控制:
-w, --interactive, --confirmation
每次操作都要求確認
兼容性選項:
-o 創建歸檔時,相當於
--old-archive;展開歸檔時,相當於
--no-same-owner
其它選項:
-?, --help 顯示此幫助列表
--restrict 禁用某些潛在的有危險的選項
--usage 顯示簡短的用法說明
--version 列印程序版本
長選項和相應短選項具有相同的強制參數或可選參數。
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control may be set with --backup or VERSION_CONTROL, values are:
none, off never make backups
t, numbered make numbered backups
nil, existing numbered if numbered backups exist, simple otherwise
never, simple always make simple backups
--quoting-style 選項的有效參數為:
literal
shell
shell-always
c
c-maybe
escape
locale
clocale
此 tar 默認為:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/usr/lib/tar/rmt
--rsh-command=/usr/bin/rsh
『貳』 如何在java中壓縮/解壓縮tar.gz文件
不能直接壓縮、解壓,可以通過調用命令行去做到
~
~
~
『叄』 如何安裝tar.gz版本的java
總體步驟是「解壓」然後「配置」,細節上講:
1解壓 :tar -zxvf java的tar.gz
2配置:vi /etc/profile //打開配置全局變數文件
export JAVA_HOME=剛才解壓的目錄
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
3.測試一下 輸入java看是不是有反應,再試試java -version能不能讀出java的版本號。
配置內容(就是export。。。)在網上有很多種,記得java_home的路徑填對就好了。這個過程只有兩部,第一步不可能出錯,所以一定是配置的問題。
『肆』 如何解壓.tar.gz gzip gz 類型文檔
java解壓縮.gz .zip .tar.gz等格式的壓縮包方法總結
1、.gz文件是linux下常見的壓縮格式。使用 java.util.zip.GZIPInputStream即可,壓縮是 java.util.zip.GZIPOutputStream
1 public static void unGzipFile(String sourcedir) {
2 String ouputfile = "";
3 try {
4 //建立gzip壓縮文件輸入流
5 FileInputStream fin = new FileInputStream(sourcedir);
6 //建立gzip解壓工作流
7 GZIPInputStream gzin = new GZIPInputStream(fin);
8 //建立解壓文件輸出流
9 ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.'));
10 ouputfile = ouputfile.substring(0,ouputfile.lastIndexOf('.'));
11 FileOutputStream fout = new FileOutputStream(ouputfile);
12
13 int num;
14 byte[] buf=new byte[1024];
15
16 while ((num = gzin.read(buf,0,buf.length)) != -1)
17 {
18 fout.write(buf,0,num);
19 }
20
21 gzin.close();
22 fout.close();
23 fin.close();
24 } catch (Exception ex){
25 System.err.println(ex.toString());
26 }
27 return;
28 }
2、zip文件,使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile
1 /**
2 * 解壓縮zipFile
3 * @param file 要解壓的zip文件對象
4 * @param outputDir 要解壓到某個指定的目錄下
5 * @throws IOException
6 */
7 public static void unZip(File file,String outputDir) throws IOException {
8 ZipFile zipFile = null;
9
10 try {
11 Charset CP866 = Charset.forName("CP866"); //specifying alternative (non UTF-8) charset
12 //ZipFile zipFile = new ZipFile(zipArchive, CP866);
13 zipFile = new ZipFile(file, CP866);
14 createDirectory(outputDir,null);//創建輸出目錄
15
16 Enumeration<?> enums = zipFile.entries();
17 while(enums.hasMoreElements()){
18
19 ZipEntry entry = (ZipEntry) enums.nextElement();
20 System.out.println("解壓." + entry.getName());
21
22 if(entry.isDirectory()){//是目錄
23 createDirectory(outputDir,entry.getName());//創建空目錄
24 }else{//是文件
25 File tmpFile = new File(outputDir + "/" + entry.getName());
26 createDirectory(tmpFile.getParent() + "/",null);//創建輸出目錄
27
28 InputStream in = null;
29 OutputStream out = null;
30 try{
31 in = zipFile.getInputStream(entry);;
32 out = new FileOutputStream(tmpFile);
33 int length = 0;
34
35 byte[] b = new byte[2048];
36 while((length = in.read(b)) != -1){
37 out.write(b, 0, length);
38 }
39
40 }catch(IOException ex){
41 throw ex;
42 }finally{
43 if(in!=null)
44 in.close();
45 if(out!=null)
46 out.close();
47 }
48 }
49 }
50
51 } catch (IOException e) {
52 throw new IOException("解壓縮文件出現異常",e);
53 } finally{
54 try{
55 if(zipFile != null){
56 zipFile.close();
57 }
58 }catch(IOException ex){
59 throw new IOException("關閉zipFile出現異常",ex);
60 }
61 }
62 }
63
64 /**
65 * 構建目錄
66 * @param outputDir
67 * @param subDir
68 */
69 public static void createDirectory(String outputDir,String subDir){
70 File file = new File(outputDir);
71 if(!(subDir == null || subDir.trim().equals(""))){//子目錄不為空
72 file = new File(outputDir + "/" + subDir);
73 }
74 if(!file.exists()){
75 if(!file.getParentFile().exists())
76 file.getParentFile().mkdirs();
77 file.mkdirs();
78 }
79 }
3、.tar.gz文件可以看做先用tar打包,再使用gz進行壓縮。
使用org.apache.tools.tar.TarEntry; org.apache.tools.tar.TarInputStream 和 org.apache.tools.tar.TarOutputStream
1 //------------------------------------------------------------------------------------------------------
2 /**
3 * 解壓tar.gz 文件
4 * @param file 要解壓的tar.gz文件對象
5 * @param outputDir 要解壓到某個指定的目錄下
6 * @throws IOException
7 */
8 public static void unTarGz(File file,String outputDir) throws IOException{
9 TarInputStream tarIn = null;
10 try{
11 tarIn = new TarInputStream(new GZIPInputStream(
12 new BufferedInputStream(new FileInputStream(file))),
13 1024 * 2);
14
15 createDirectory(outputDir,null);//創建輸出目錄
16
17 TarEntry entry = null;
18 while( (entry = tarIn.getNextEntry()) != null ){
19
20 if(entry.isDirectory()){//是目錄
21 entry.getName();
22 createDirectory(outputDir,entry.getName());//創建空目錄
23 }else{//是文件
24 File tmpFile = new File(outputDir + "/" + entry.getName());
25 createDirectory(tmpFile.getParent() + "/",null);//創建輸出目錄
26 OutputStream out = null;
27 try{
28 out = new FileOutputStream(tmpFile);
29 int length = 0;
30
31 byte[] b = new byte[2048];
32
33 while((length = tarIn.read(b)) != -1){
34 out.write(b, 0, length);
35 }
36
37 }catch(IOException ex){
38 throw ex;
39 }finally{
40
41 if(out!=null)
42 out.close();
43 }
44 }
45 }
46 }catch(IOException ex){
47 throw new IOException("解壓歸檔文件出現異常",ex);
48 } finally{
49 try{
50 if(tarIn != null){
51 tarIn.close();
52 }
53 }catch(IOException ex){
54 throw new IOException("關閉tarFile出現異常",ex);
55 }
56 }
57 }
使用到的包頭有:
1 import java.io.BufferedInputStream;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import java.nio.charset.Charset;
10 import java.util.Enumeration;
11 import java.util.zip.GZIPInputStream;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipFile;
14
15 import org.apache.tools.tar.TarEntry;
16 import org.apache.tools.tar.TarInputStream;
17 import org.apache.tools.tar.TarOutputStream;
『伍』 java解壓tar.gz用到哪些包
//引入jtar-(版本號).jar
publicvoidreadtar()throwsIOException{
StringtarFile="D:/20120725.tar.gz";
StringdestFolder="D:/20120725";
Filess=newFile(tarFile);
TarInputStreamtis=null;
tis=newTarInputStream(newGZIPInputStream(newBufferedInputStream(newFileInputStream(ss))));
TarEntryentry;
try{
while((entry=tis.getNextEntry())!=null){
intcount;
bytedata[]=newbyte[204800];
FileOutputStreamfos=newFileOutputStream(newFile(destFolder+"/"+entry.getName()));
BufferedOutputStreamdest=newBufferedOutputStream(fos);
while((count=tis.read(data))!=-1){
dest.write(data,0,count);
}
dest.flush();
dest.close();
}
tis.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
『陸』 eclipse 官網下載 tar.gz包,解壓後打不開。JDK沒問題
jdk你配置環境變數了沒,還有可能是你在下載的時候出現丟包的現象了,你的eclipse沒有全部下載好文件。有損壞的,再下個試試,或者安裝myeclipse
『柒』 java里怎麼解壓tar.gz文件啊,網上好多例子都不行
最後怎麼解決的,我現在也遇到這個問題了,單個文件可以解壓可以壓縮,寫入的測試內容也在,換成文件夾就不行了。能找到的案例全都是解壓成文件,但是本身是個文件夾的GZ包解壓了以後也打不開。
『捌』 java 如何用zlib解壓縮tar.gz文件
public static void makeZip(List<File> fileList,String zipPath,boolean isDelete) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
File zipFile = new File(zipPath);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
// Compress the files
for (int i = 0; i < fileList.size(); i++) {
FileInputStream in = new FileInputStream(fileList.get(i));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileList.get(i).getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
System.out.println("壓縮完成.");
//把舊的文件刪除
if(isDelete == true){
for (int i = 0; i < fileList.size(); i++) {
File oldFile = fileList.get(i);
oldFile.delete();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
File in1=new File("D:\\a.txt");
File in2=new File("D:\\b.txt");
File[] file=new File[]{in1,in2};
File zip=new File("D:\\ab.zip");
IDMZip mgr=new IDMZip();
mgr.ZipFiles(file, zip);
}
這個方法不管你是在windows下還是在linux下,都能正常執行。
『玖』 Java 怎麼解壓zlib函數庫壓縮的tar.gz格式的文件
這個文件不是在windows環境下解壓的,它是在linux系統下的壓縮文件。你應該解壓zip格式的壓縮包
『拾』 gz後綴怎麼解壓
1、在Windows系統環境下,安裝解壓軟體