① 如何解压.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都能直接解压。