当前位置:首页 » 文件管理 » 解压gzip

解压gzip

发布时间: 2022-02-15 13:14:41

1. 求后缀gzip的文件怎么打开或解压

你可以下载一个7-zip 这是一个解压文件~~!下完后,在点文件文件时,他会出现。点open....在点击蹦出来的文件,就行了!

2. 如何解压gz文件

当在备份重要文件和通过网络发送大文件的时候,对文件进行压缩非常有用。请注意,压缩一个已经压缩过的文件会增加额外开销,因此你将会得到一个更大一些的文件。所以,请不要压缩已经压缩过的文件。在 GNU/linux 中,有许多程序可以用来压缩和解压缩文件。在这篇教程中,我们仅学习其中两个应用程序。

在类 Unix 系统中,最常见的用来压缩文件的程序是:

  • gzip

  • bzip2

  • 1. 使用 gzip 程序来压缩和解压缩文件

    gzip是一个使用 Lempel-Ziv 编码(LZ77)算法来压缩和解压缩文件的实用工具。

    1.1 压缩文件

    如果要压缩一个名为ostechnix.txt的文件,使之成为 gzip 格式的压缩文件,那么只需运行如下命令:

  • $ gzip ostechnix.txt

  • 上面的命令运行结束之后,将会出现一个名为ostechnix.txt.gz的 gzip 格式压缩文件,代替了原始的ostechnix.txt文件。

    gzip命令还可以有其他用法。一个有趣的例子是,我们可以将一个特定命令的输出通过管道传递,然后作为gzip程序的输入来创建一个压缩文件。看下面的命令:

  • $ ls -l Downloads/ | gzip > ostechnix.txt.gz

  • 上面的命令将会创建一个 gzip 格式的压缩文件,文件的内容为Downloads目录的目录项。

    1.2 压缩文件并将输出写到新文件中(不覆盖原始文件)

    默认情况下,gzip程序会压缩给定文件,并以压缩文件替代原始文件。但是,你也可以保留原始文件,并将输出写到标准输出。比如,下面这个命令将会压缩ostechnix.txt文件,并将输出写入文件output.txt.gz。

  • $ gzip -c ostechnix.txt > output.txt.gz

  • 类似地,要解压缩一个gzip格式的压缩文件并指定输出文件的文件名,只需运行:

  • $ gzip -c -d output.txt.gz > ostechnix1.txt

  • 上面的命令将会解压缩output.txt.gz文件,并将输出写入到文件ostechnix1.txt中。在上面两个例子中,原始文件均不会被删除。

    1.3 解压缩文件

    如果要解压缩ostechnix.txt.gz文件,并以原始未压缩版本的文件来代替它,那么只需运行:

  • $ gzip -d ostechnix.txt.gz

  • 我们也可以使用gunzip程序来解压缩文件:

  • $ gunzip ostechnix.txt.gz

  • 1.4 在不解压缩的情况下查看压缩文件的内容

    如果你想在不解压缩的情况下,使用gzip程序查看压缩文件的内容,那么可以像下面这样使用-c选项:

  • $ gunzip -c ostechnix1.txt.gz

  • 或者,你也可以像下面这样使用zcat程序:

  • $ zcat ostechnix.txt.gz

  • 你也可以通过管道将输出传递给less命令,从而一页一页的来查看输出,就像下面这样:

  • $ gunzip -c ostechnix1.txt.gz | less

  • $ zcat ostechnix.txt.gz | less

  • 另外,zless程序也能够实现和上面的管道同样的功能。

  • $ zless ostechnix1.txt.gz

  • 1.5 使用 gzip 压缩文件并指定压缩级别

    gzip的另外一个显着优点是支持压缩级别。它支持下面给出的 3 个压缩级别:

  • 1– 最快 (最差)

  • 9– 最慢 (最好)

  • 6– 默认级别

  • 要压缩名为ostechnix.txt的文件,使之成为“最好”压缩级别的 gzip 压缩文件,可以运行:

  • $ gzip -9 ostechnix.txt

  • 1.6 连接多个压缩文件

    我们也可以把多个需要压缩的文件压缩到同一个文件中。如何实现呢?看下面这个例子。

  • $ gzip -c ostechnix1.txt > output.txt.gz

  • $ gzip -c ostechnix2.txt >> output.txt.gz

  • 上面的两个命令将会压缩文件ostechnix1.txt和ostechnix2.txt,并将输出保存到一个文件output.txt.gz中。

    你可以通过下面其中任何一个命令,在不解压缩的情况下,查看两个文件ostechnix1.txt和ostechnix2.txt的内容:

  • $ gunzip -c output.txt.gz

  • $ gunzip -c output.txt

  • $ zcat output.txt.gz

  • $ zcat output.txt

  • 如果你想了解关于gzip的更多细节,请参阅它的 man 手册。

  • $ man gzip

  • 2. 使用 bzip2 程序来压缩和解压缩文件

    bzip2和gzip非常类似,但是bzip2使用的是 Burrows-Wheeler 块排序压缩算法,并使用哈夫曼(Huffman)编码。使用bzip2压缩的文件以 “.bz2” 扩展结尾。

    正如我上面所说的,bzip2的用法和gzip几乎完全相同。只需在上面的例子中将gzip换成bzip2,将gunzip换成bunzip2,将zcat换成bzcat即可。

    要使用bzip2压缩一个文件,并以压缩后的文件取而代之,只需运行:

  • $ bzip2 ostechnix.txt

  • 如果你不想替换原始文件,那么可以使用-c选项,并把输出写入到新文件中。

  • $ bzip2 -c ostechnix.txt > output.txt.bz2

  • 如果要解压缩文件,则运行:

  • $ bzip2 -d ostechnix.txt.bz2

  • 或者,

  • $ bunzip2 ostechnix.txt.bz2

  • 如果要在不解压缩的情况下查看一个压缩文件的内容,则运行:

  • $ bunzip2 -c ostechnix.txt.bz2

  • 或者,

  • $ bzcat ostechnix.txt.bz2

  • 如果你想了解关于bzip2的更多细节,请参阅它的 man 手册。

  • $ man bzip2

  • 总结

    在这篇教程中,我们学习了gzip和bzip2程序是什么,并通过 GNU/Linux 下的一些例子学习了如何使用它们来压缩和解压缩文件。接下来,我们将要学习如何在 Linux 中将文件和目录归档。

    干杯!

3. GZIP 与zip区别

一、主体不同

1、GZIP:最早由Jean-loup Gailly和Mark Adler创建,用于UNⅨ系统的文件压缩。

2、ZIP:ZIP文件格式是一种数据压缩和文档储存的文件格式。

二、特点不同

1、GZIP:基础是DEFLATE,DEFLATE是LZ77与哈夫曼编码的一个组合体。DEFLATE最初是作为LZW以及其它受专利保护的数据压缩算法的替代版本而设计的。

2、ZIP:是一种相当简单的分别压缩每个文件的存档格式。分别压缩文件允许不必读取另外的数据而检索独立的文件。


三、优点不同

1、GZIP:可以减少存储空间,通过网络传输文件时,可以减少传输的时间。

2、ZIP:支持基于对称加密系统的一个简单的密码,已知有严重的缺陷,已知明文攻击,字典攻击和暴力攻击。

4. zip.gzip.zip用什么解压

多层压缩,Linux下先用unzip解压,再用

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 解压
然后再用unzip解压
windows下就winrar软件

5. .rar.gzip文件怎么解压

先解压gzip再看rar是扩展名还是文件名,是扩展名再解压

6. 急!如何解压rar.gzip格式的文件,谢谢!

只有解压RAR和ZIP的,没有gzip的。用RAR可以解压RAR和ZIP。gzip估计就别人改了后置名的,正常的只有zip的

7. 如何用zlib解压gzip数据

class CGZIP
{
public:

LPGZIP pgzip;

int Length;
public:

CGZIP(char* lpsz, Memory *pool, int len = -1):pgzip(0),Length(0)

{

this->m_pool = pool;

Init(lpsz,len);

}

~CGZIP()

{

if(pgzip!=m_buffer) TRYFREE(pgzip);

}

void Init(char *lpsz,int len=-1)

{

if(lpsz==0)

{

pgzip=0;

Length=0;

return ;

}

if(len==-1)

{

len=(int)strlen(lpsz);

}

m_CurrentBufferSize=BufLength;

pgzip=m_buffer;

m_zstream.zalloc = (alloc_func)0;

m_zstream.zfree = (free_func)0;

m_zstream.opaque = (voidpf)0;

m_zstream.next_in = Z_NULL;

m_zstream.next_out = Z_NULL;

m_zstream.avail_in = 0;

m_zstream.avail_out = 0;

m_z_err = Z_OK;

m_crc = crc32(0L, Z_NULL, 0);

int err = deflateInit2(&(m_zstream), t_nLevel,Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, t_nStrategy);

m_outbuf = (Byte*)ALLOC(Z_BUFSIZE);

m_zstream.next_out = m_outbuf;

if (err != Z_OK || m_outbuf == Z_NULL)

{

destroy();

return ;

}

m_zstream.avail_out = Z_BUFSIZE;

GZIP header[10]={0x1f,0x8b,Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE};

write(header,10);

m_zstream.next_in = (Bytef*)lpsz;

m_zstream.avail_in = len;

while (m_zstream.avail_in != 0)

{

if (m_zstream.avail_out == 0)

{

m_zstream.next_out = m_outbuf;

this->write(m_outbuf,Z_BUFSIZE);

m_zstream.avail_out = Z_BUFSIZE;

}

m_z_err = deflate(&m_zstream,Z_NO_FLUSH);

if (m_z_err != Z_OK) break;

}

m_crc = crc32(m_crc, (const Bytef *)lpsz, len);

if (finish() != Z_OK) { destroy(); return ;}

putLong(m_crc);

putLong (m_zstream.total_in);

destroy();

}
private:

GZIP m_buffer[BufLength];

int m_CurrentBufferSize;

Memory *m_pool;

z_stream m_zstream;

int m_z_err; /* error code for last stream operation */

Byte *m_outbuf; /* output buffer */

uLong m_crc; /* crc32 of uncompressed data */

int write(LPGZIP buf,int count)

{

if(buf==0) return 0;

if(Length+count>m_CurrentBufferSize)

{

int nTimes=(Length+count)/BufLength +1;

LPGZIP pTemp=pgzip;

pgzip=static_cast<LPGZIP>( malloc(nTimes*BufLength));

m_CurrentBufferSize=nTimes*BufLength;

System::IO::Memory::memcpy(pgzip,pTemp,Length);

if(pTemp!=m_buffer) free(pTemp);

}

System::IO::Memory::memcpy(pgzip+Length,buf,count);

Length+=count;

return count;

}

int finish()

{

uInt len;

int done = 0;

m_zstream.avail_in = 0;

for (;;)

{

len = Z_BUFSIZE - m_zstream.avail_out;

if (len != 0)

{

write(m_outbuf,len);

m_zstream.next_out = m_outbuf;

m_zstream.avail_out = Z_BUFSIZE;

}

if (done) break;

m_z_err = deflate(&(m_zstream), Z_FINISH);

if (len == 0 && m_z_err == Z_BUF_ERROR) m_z_err = Z_OK;

done = (m_zstream.avail_out != 0 || m_z_err == Z_STREAM_END);

if (m_z_err != Z_OK && m_z_err != Z_STREAM_END) break;

}

return m_z_err == Z_STREAM_END ? Z_OK : m_z_err;

}

int destroy()

{

int err = Z_OK;

if (m_zstream.state != NULL) {

err = deflateEnd(&(m_zstream));

}

if (m_z_err < 0) err = m_z_err;

TRYFREE(m_outbuf);

return err;

}

void putLong (uLong x)

{

for(int n = 0; n < 4; n++) {

unsigned char c=(unsigned char)(x & 0xff);

write(&c,1);

x >>= 8;

}

}
};
class UNGZIP
{
public:

char *psz;

int Length;

UNGZIP(LPGZIP pgzip, int len, Memory *pool):m_gzip(pgzip),m_gziplen(len),psz(0),Length(0),m_pos(0)

{

this->m_pool = pool;

Init();

}

~UNGZIP()

{

if(psz!=m_buffer) TRYFREE(psz);

}

void Init()

{

if(m_gzip==0)

{

psz=0;

Length=0;

return ;

}

m_CurrentBufferSize=BufLength;

psz=m_buffer;

System::IO::Memory::memset(psz,0,m_CurrentBufferSize+1);

m_zstream.zalloc = (alloc_func)0;

m_zstream.zfree = (free_func)0;

m_zstream.opaque = (voidpf)0;

m_zstream.next_in = m_inbuf = Z_NULL;

m_zstream.next_out = Z_NULL;

m_zstream.avail_in = m_zstream.avail_out = 0;

m_z_err = Z_OK;

m_z_eof = 0;

m_transparent = 0;

m_crc = crc32(0L, Z_NULL, 0);

m_zstream.next_in =m_inbuf = (Byte*)ALLOC(Z_BUFSIZE);

int err = inflateInit2(&(m_zstream), -MAX_WBITS);

if (err != Z_OK || m_inbuf == Z_NULL)

{

destroy();

return;

}

m_zstream.avail_out = Z_BUFSIZE;

check_header();

char outbuf[Z_BUFSIZE];

int nRead;

while((nRead = gzread(outbuf,Z_BUFSIZE)) > 0)

{

write(outbuf,nRead);

}

destroy();

}
private:

char m_buffer[BufLength+1];

int m_CurrentBufferSize;

Memory *m_pool;

z_stream m_zstream;

int m_z_err; /* error code for last stream operation */

Byte *m_inbuf; /* output buffer */

uLong m_crc; /* crc32 of uncompressed data */

int m_z_eof;

int m_transparent;

int m_pos;

LPGZIP m_gzip;

int m_gziplen;

void check_header()

{

int method; /* method byte */

int flags; /* flags byte */

uInt len;

int c;

/* Check the gzip magic header */

for (len = 0; len < 2; len++) {

c = get_byte();

if (c != gz_magic[len]) {

if (len != 0) m_zstream.avail_in++, m_zstream.next_in--;

if (c != EOF) {

m_zstream.avail_in++, m_zstream.next_in--;

m_transparent = 1;

}

m_z_err =m_zstream.avail_in != 0 ? Z_OK : Z_STREAM_END;

return;

}

}

method = get_byte();

flags = get_byte();

if (method != Z_DEFLATED || (flags & RESERVED) != 0) {

m_z_err = Z_DATA_ERROR;

return;

}

/* Discard time, xflags and OS code: */

for (len = 0; len < 6; len++) (void)get_byte();

if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */

len = (uInt)get_byte();

len += ((uInt)get_byte())<<8;

/* len is garbage if EOF but the loop below will quit anyway */

while (len-- != 0 && get_byte() != EOF) ;

}

if ((flags & ORIG_NAME) != 0) { /* skip the original file name */

while ((c = get_byte()) != 0 && c != EOF) ;

}

if ((flags & COMMENT) != 0) { /* skip the .gz file comment */

while ((c = get_byte()) != 0 && c != EOF) ;

}

if ((flags & HEAD_CRC) != 0) { /* skip the header crc */

for (len = 0; len < 2; len++) (void)get_byte();

}

m_z_err = m_z_eof ? Z_DATA_ERROR : Z_OK;

}

int get_byte()

{

if (m_z_eof) return EOF;

if (m_zstream.avail_in == 0)

{

errno = 0;

m_zstream.avail_in =read(m_inbuf,Z_BUFSIZE);

if(m_zstream.avail_in == 0)

{

m_z_eof = 1;

return EOF;

}

m_zstream.next_in = m_inbuf;

}

m_zstream.avail_in--;

return *(m_zstream.next_in)++;

}

int read(LPGZIP buf,int size)

{

int nRead=size;

if(m_pos+size>=m_gziplen)

{

nRead=m_gziplen-m_pos;

}

if(nRead<=0) return 0;

System::IO::Memory::memcpy(buf,m_gzip+m_pos,nRead);

m_pos+=nRead;

return nRead;

}

int gzread(char* buf,int len)

{

Bytef *start = (Bytef*)buf; /* starting point for crc computation */

Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */

if (m_z_err == Z_DATA_ERROR || m_z_err == Z_ERRNO) return -1;

if (m_z_err == Z_STREAM_END) return 0; /* EOF */

next_out = (Byte*)buf;

m_zstream.next_out = (Bytef*)buf;

m_zstream.avail_out = len;

while (m_zstream.avail_out != 0) {

if (m_transparent)

{

/* Copy first the lookahead bytes: */

uInt n = m_zstream.avail_in;

if (n > m_zstream.avail_out) n = m_zstream.avail_out;

if (n > 0)

{

zmemcpy(m_zstream.next_out,m_zstream.next_in, n);

next_out += n;

m_zstream.next_out = next_out;

m_zstream.next_in += n;

m_zstream.avail_out -= n;

m_zstream.avail_in -= n;

}

if (m_zstream.avail_out > 0) {

m_zstream.avail_out -=read(next_out,m_zstream.avail_out);

}

len -= m_zstream.avail_out;

m_zstream.total_in += (uLong)len;

m_zstream.total_out += (uLong)len;

if (len == 0) m_z_eof = 1;

return (int)len;

}

if (m_zstream.avail_in == 0 && !m_z_eof)

{

errno = 0;

m_zstream.avail_in = read(m_inbuf,Z_BUFSIZE);

if (m_zstream.avail_in == 0)

{

m_z_eof = 1;

}

m_zstream.next_in = m_inbuf;

}

m_z_err = inflate(&(m_zstream), Z_NO_FLUSH);

if (m_z_err == Z_STREAM_END)

{

/* Check CRC and original size */

m_crc = crc32(m_crc, start, (uInt)(m_zstream.next_out - start));

start = m_zstream.next_out;

if (getLong() != m_crc) {

m_z_err = Z_DATA_ERROR;

}else

{

(void)getLong();

check_header();

if (m_z_err == Z_OK)

{

uLong total_in = m_zstream.total_in;

uLong total_out = m_zstream.total_out;

inflateReset(&(m_zstream));

m_zstream.total_in = total_in;

m_zstream.total_out = total_out;

m_crc = crc32(0L, Z_NULL, 0);

}

}

}

if (m_z_err != Z_OK || m_z_eof) break;

}

m_crc = crc32(m_crc, start, (uInt)(m_zstream.next_out - start));

return (int)(len - m_zstream.avail_out);

}

uLong getLong()

{

uLong x = (uLong)get_byte();

int c;

x += ((uLong)get_byte())<<8;

x += ((uLong)get_byte())<<16;

c = get_byte();

if (c == EOF) m_z_err = Z_DATA_ERROR;

x += ((uLong)c)<<24;

return x;

}

int write(char* buf,int count)

{

if(buf==0) return 0;

if(Length+count>m_CurrentBufferSize)

{

int nTimes=(Length+count)/BufLength +1;

char *pTemp=psz;

psz=static_cast<char*>( malloc(nTimes*BufLength+1));

m_CurrentBufferSize=nTimes*BufLength;

Memory::memset(psz,0,m_CurrentBufferSize+1);

Memory::memcpy(psz,pTemp,Length);

if(pTemp!=m_buffer) free(pTemp);

}

Memory::memcpy(psz+Length,buf,count);

Length+=count;

return count;

}

int destroy()

{

int err = Z_OK;

if (m_zstream.state != NULL) {

err = inflateEnd(&(m_zstream));

}

if (m_z_err < 0) err = m_z_err;

TRYFREE(m_inbuf);

return err;

}
};

8. 被压缩为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();
}
}
}

9. 怎样一起解压两个gzip文件

你可以把后缀直接删除,保留1.rar再试试能不能解压,如果不行,你再上载gzip的解压工具。例如:7zip

10. 如何解压.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;

热点内容
rar解压缩破解版 发布:2024-10-26 00:12:43 浏览:380
javarestfulapi开发 发布:2024-10-26 00:04:04 浏览:443
服务器设置业务口ip 发布:2024-10-25 23:46:12 浏览:69
手机游戏解压缩怎么用 发布:2024-10-25 23:45:57 浏览:255
存储过程性能 发布:2024-10-25 23:35:39 浏览:111
同一个ip是不是同一个服务器 发布:2024-10-25 23:33:15 浏览:518
为啥c语言要编译器啊 发布:2024-10-25 23:32:38 浏览:218
快存储 发布:2024-10-25 23:31:05 浏览:286
算法制造 发布:2024-10-25 23:11:27 浏览:886
台式电脑安装哪个安卓系统好 发布:2024-10-25 23:11:25 浏览:114