① 如何解壓.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;
② 請問後綴為gzip的文件如何打開
GZIP最早由Jean-loup Gailly和Mark Adler創建,用於UNIX系統的文件壓縮。我們在Linux中經常會用到後綴為.gz的文件,它們就是GZIP格式的。現今已經成為Internet 上使用非常普遍的一種數據壓縮格式,或者說一種文件格式。HTTP協議上的GZIP編碼是一種用來改進WEB應用程序性能的技術。大流量的WEB站點常常使用GZIP壓縮技術來讓用戶感受更快的速度。
gzip 命令
減少文件大小有兩個明顯的好處,一是可以減少存儲空間,二是通過網路傳輸文件時,可以減少傳輸的時間。gzip 是在 Linux 系統中經常使用的一個對文件進行壓縮和解壓縮的命令,既方便又好用。
語法:gzip [選項] 壓縮(解壓縮)的文件名
該命令的各選項含義如下:
-c 將輸出寫到標准輸出上,並保留原有文件。
-d 將壓縮文件解壓。
-l 對每個壓縮文件,顯示下列欄位:
壓縮文件的大小;未壓縮文件的大小;壓縮比;未壓縮文件的名字
-r 遞歸式地查找指定目錄並壓縮其中的所有文件或者是解壓縮。
-t 測試,檢查壓縮文件是否完整。
-v 對每一個壓縮和解壓的文件,顯示文件名和壓縮比。
-num 用指定的數字 num 調整壓縮的速度,-1 或 --fast 表示最快壓縮方法(低壓縮比),
-9 或--best表示最慢壓縮方法(高壓縮比)。系統預設值為 6。
指令實例:
gzip *
% 把當前目錄下的每個文件壓縮成 .gz 文件。
gzip -dv *
% 把當前目錄下每個壓縮的文件解壓,並列出詳細的信息。
gzip -l *
% 詳細顯示例1中每個壓縮的文件的信息,並不解壓。
gzip usr.tar
% 壓縮 tar 備份文件 usr.tar,此時壓縮文件的擴展名為.tar.gz。
③ gzip 文件 怎麼打開在 windows7 系統中
1、使用WinRAR打開
gzip是GNUzip的縮寫,它是一個GNU自由軟體的文件壓縮程序,在Linux上這種類型的壓縮文件較常見。現今已經成為Internet 上使用非常普遍的一種數據壓縮格式,或者說一種文件格式。HTTP協議上的GZIP編碼是一種用來改進WEB應用程序性能的技術。大流量的WEB站點常常使用GZIP壓縮技術來讓用戶感受更快的速度。
④ 7z文件手機怎麼解壓
打開【QQ瀏覽器】,點擊界面下方的【文件】,選擇【壓縮包】,打開要解壓的7z文件,點擊【解壓全部文件】或文件旁邊的解壓,再點擊【去看看】即可查看文件。
手機是可以在較廣范圍內使用的攜帶型電話終端,全稱為行動電話或無線電話,最初只是一種通訊工具,早期在中國有「大哥大」的俗稱。
手機最早是由美國貝爾實驗室於1940年製造的戰地行動電話機發展而來,後美國摩托羅拉工程師馬丁·庫帕於1973年發明了世界上第一部商業化手機。現代的手機除了典型的電話功能外,還包含了照相機、GPS和連接互聯網等更多功能,它們都概括性地被稱作智能手機。
⑤ gzip怎麼壓縮和怎麼解壓縮文件到其他目錄
解決:gzip -c test.txt > /root/test.gz,文件流重定向,解壓也是,gunzip -c /root/test.gz > ./test.txt
經驗:更常用的命令tar同樣可以解壓*.gz,參數為-c
附gzip幫助文件
GZIP(1) General Commands Manual GZIP(1)
NAME
gzip, gunzip, zcat - compress or expand files
SYNOPSIS
gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ]
gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ]
zcat [ -fhLV ] [ name ... ]
OPTIONS
-a --ascii
Ascii text mode: convert end-of-lines using local conventions.
This option is supported only on some non-Unix systems. For
MSDOS, CR LF is converted to LF when compressing, and LF is con‐
verted to CR LF when decompressing.
-c --stdout --to-stdout
Write output on standard output; keep original files unchanged.
If there are several input files, the output consists of a
sequence of independently compressed members. To obtain better
compression, concatenate all input files before compressing
them.
-d --decompress --uncompress
Decompress.
-f --force
Force compression or decompression even if the file has multiple
links or the corresponding file already exists, or if the com‐
pressed data is read from or written to a terminal. If the input
data is not in a format recognized by gzip, and if the option
--stdout is also given, the input data without change to
the standard output: let zcat behave as cat. If -f is not
given, and when not running in the background, gzip prompts to
verify whether an existing file should be overwritten.
-h --help
Display a help screen and quit.
-l --list
For each compressed file, list the following fields:
compressed size: size of the compressed file
uncompressed size: size of the uncompressed file
ratio: compression ratio (0.0% if unknown)
uncompressed_name: name of the uncompressed file
The uncompressed size is given as -1 for files not in gzip for‐
mat, such as compressed .Z files. To get the uncompressed size
for such a file, you can use:
zcat file.Z | wc -c
In combination with the --verbose option, the following fields
are also displayed:
method: compression method
crc: the 32-bit CRC of the uncompressed data
date & time: time stamp for the uncompressed file
The compression methods currently supported are deflate, com‐
press, lzh (SCO compress -H) and pack. The crc is given as
ffffffff for a file not in gzip format.
With --name, the uncompressed name, date and time are those
stored within the compress file if present.
With --verbose, the size totals and compression ratio for all
files is also displayed, unless some sizes are unknown. With
--quiet, the title and totals lines are not displayed.
-L --license
Display the gzip license and quit.
-n --no-name
When compressing, do not save the original file name and time
stamp by default. (The original name is always saved if the name
had to be truncated.) When decompressing, do not restore the
original file name if present (remove only the gzip suffix from
the compressed file name) and do not restore the original time
stamp if present ( it from the compressed file). This option
is the default when decompressing.
-N --name
When compressing, always save the original file name and time
stamp; this is the default. When decompressing, restore the
original file name and time stamp if present. This option is
useful on systems which have a limit on file name length or when
the time stamp has been lost after a file transfer.
-q --quiet
Suppress all warnings.
-r --recursive
Travel the directory structure recursively. If any of the file
names specified on the command line are directories, gzip will
descend into the directory and compress all the files it finds
there (or decompress them in the case of gunzip ).
-S .suf --suffix .suf
When compressing, use suffix .suf instead of .gz. Any non-empty
suffix can be given, but suffixes other than .z and .gz should
be avoided to avoid confusion when files are transferred to
other systems.
When decompressing, add .suf to the beginning of the list of
suffixes to try, when deriving an output file name from an input
file name.
pack(1).
-t --test
Test. Check the compressed file integrity.
-v --verbose
Verbose. Display the name and percentage rection for each file
compressed or decompressed.
-V --version
Version. Display the version number and compilation options then
quit.
-# --fast --best
Regulate the speed of compression using the specified digit #,
where -1 or --fast indicates the fastest compression method
(less compression) and -9 or --best indicates the slowest com‐
pression method (best compression). The default compression
level is -6 (that is, biased towards high compression at expense
of speed).
⑥ 安卓手機怎麼解壓壓縮包ZIP,RAR和tar格式文件及打包
安卓手機可以對zip.、.rar後綴的壓縮包文件進行解壓,具體步驟如下:
在網站上查找「安卓解壓軟體」,下載並安裝解壓apk程序。
安裝後,在手機「文件管理」中找到需解壓的文件,點擊後進行解壓或長按需解壓的文件,選擇解壓軟體進行解壓。
壓縮軟體進行解壓的原理就是把二進制信息中相同的字元串以特殊字元標記來達到壓縮的目的。
解壓:將壓縮文件還原為本來的文件格式,也稱釋放、擴展。
壓縮包:一般將通用壓縮格式的文件稱為壓縮包,如zip格式壓縮文件。這種文件可以在壓縮工具的管理下對包中壓縮的文件進行管理,如查看、刪除、添加等。
壓縮文件有很多格式,目前主要是:.zip,.rar,.arj,.cab
⑦ 被壓縮為gzip格式的二進制數組如何解壓
直接編譯運行!!!
不知道你是要查看壓縮文件還是要解壓文件,所以發上來兩個。
第一個可以查看各個壓縮項目;
第二個可以解壓文件。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
class ZipTest {
public static void main(String[] args) {
ZipTestFrame frame = new ZipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ZipTestFrame extends JFrame {
private JComboBox fileCombo;
private JTextArea fileText;
private String zipname;
public ZipTestFrame() {
setTitle("ZipTest");
setSize(400,300);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new OpenAction());
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
menuBar.add(menu);
setJMenuBar(menuBar);
fileText = new JTextArea();
fileCombo = new JComboBox();
fileCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
loadZipFile((String)fileCombo.getSelectedItem());
}
});
add(fileCombo, BorderLayout.SOUTH);
add(new JScrollPane(fileText), BorderLayout.CENTER);
}
public class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension(".zip");
filter.addExtension(".jar");
filter.setDescription("ZIP archives");
chooser.setFileFilter(filter);
int r = chooser.showOpenDialog(ZipTestFrame.this);
if(r == JFileChooser.APPROVE_OPTION) {
zipname = chooser.getSelectedFile().getPath();
scanZipFile();
}
}
}
⑧ 求後綴gzip的文件怎麼打開或解壓。
你可以下載一個7-zip 這是一個解壓文件~~!下完後,在點文件文件時,他會出現。點open....在點擊蹦出來的文件,就行了!
⑨ 如何解壓gz文件
當在備份重要文件和通過網路發送大文件的時候,對文件進行壓縮非常有用。請注意,壓縮一個已經壓縮過的文件會增加額外開銷,因此你將會得到一個更大一些的文件。所以,請不要壓縮已經壓縮過的文件。在 GNU/Linux 中,有許多程序可以用來壓縮和解壓縮文件。在這篇教程中,我們僅學習其中兩個應用程序。
在類 Unix 系統中,最常見的用來壓縮文件的程序是:
gzip
bzip2
1. 使用 gzip 程序來壓縮和解壓縮文件
gzip是一個使用 Lempel-Ziv 編碼(LZ77)演算法來壓縮和解壓縮文件的實用工具。
1.1 壓縮文件
如果要壓縮一個名為ostechnix.txt的文件,使之成為 gzip 格式的壓縮文件,那麼只需運行如下命令:
上面的命令運行結束之後,將會出現一個名為ostechnix.txt.gz的 gzip 格式壓縮文件,代替了原始的ostechnix.txt文件。
gzip命令還可以有其他用法。一個有趣的例子是,我們可以將一個特定命令的輸出通過管道傳遞,然後作為gzip程序的輸入來創建一個壓縮文件。看下面的命令:
上面的命令將會創建一個 gzip 格式的壓縮文件,文件的內容為Downloads目錄的目錄項。
1.2 壓縮文件並將輸出寫到新文件中(不覆蓋原始文件)
默認情況下,gzip程序會壓縮給定文件,並以壓縮文件替代原始文件。但是,你也可以保留原始文件,並將輸出寫到標准輸出。比如,下面這個命令將會壓縮ostechnix.txt文件,並將輸出寫入文件output.txt.gz。
類似地,要解壓縮一個gzip格式的壓縮文件並指定輸出文件的文件名,只需運行:
上面的命令將會解壓縮output.txt.gz文件,並將輸出寫入到文件ostechnix1.txt中。在上面兩個例子中,原始文件均不會被刪除。
1.3 解壓縮文件
如果要解壓縮ostechnix.txt.gz文件,並以原始未壓縮版本的文件來代替它,那麼只需運行:
我們也可以使用gunzip程序來解壓縮文件:
1.4 在不解壓縮的情況下查看壓縮文件的內容
如果你想在不解壓縮的情況下,使用gzip程序查看壓縮文件的內容,那麼可以像下面這樣使用-c選項:
或者,你也可以像下面這樣使用zcat程序:
你也可以通過管道將輸出傳遞給less命令,從而一頁一頁的來查看輸出,就像下面這樣:
另外,zless程序也能夠實現和上面的管道同樣的功能。
1.5 使用 gzip 壓縮文件並指定壓縮級別
gzip的另外一個顯著優點是支持壓縮級別。它支持下面給出的 3 個壓縮級別:
1– 最快 (最差)
9– 最慢 (最好)
6– 默認級別
要壓縮名為ostechnix.txt的文件,使之成為「最好」壓縮級別的 gzip 壓縮文件,可以運行:
1.6 連接多個壓縮文件
我們也可以把多個需要壓縮的文件壓縮到同一個文件中。如何實現呢?看下面這個例子。
上面的兩個命令將會壓縮文件ostechnix1.txt和ostechnix2.txt,並將輸出保存到一個文件output.txt.gz中。
你可以通過下面其中任何一個命令,在不解壓縮的情況下,查看兩個文件ostechnix1.txt和ostechnix2.txt的內容:
如果你想了解關於gzip的更多細節,請參閱它的 man 手冊。
2. 使用 bzip2 程序來壓縮和解壓縮文件
bzip2和gzip非常類似,但是bzip2使用的是 Burrows-Wheeler 塊排序壓縮演算法,並使用哈夫曼(Huffman)編碼。使用bzip2壓縮的文件以 「.bz2」 擴展結尾。
正如我上面所說的,bzip2的用法和gzip幾乎完全相同。只需在上面的例子中將gzip換成bzip2,將gunzip換成bunzip2,將zcat換成bzcat即可。
要使用bzip2壓縮一個文件,並以壓縮後的文件取而代之,只需運行:
如果你不想替換原始文件,那麼可以使用-c選項,並把輸出寫入到新文件中。
如果要解壓縮文件,則運行:
或者,
如果要在不解壓縮的情況下查看一個壓縮文件的內容,則運行:
或者,
如果你想了解關於bzip2的更多細節,請參閱它的 man 手冊。
總結
在這篇教程中,我們學習了gzip和bzip2程序是什麼,並通過 GNU/Linux 下的一些例子學習了如何使用它們來壓縮和解壓縮文件。接下來,我們將要學習如何在 Linux 中將文件和目錄歸檔。
乾杯!
⑩ win7系統下怎樣解壓gz文件
你好,用7zip或者winrar都能直接解壓。