當前位置:首頁 » 文件管理 » 解壓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;

熱點內容
邁銳寶買哪個配置合算 發布:2024-10-25 22:28:59 瀏覽:993
押韻腳本生成器 發布:2024-10-25 22:26:50 瀏覽:368
找文檔上傳 發布:2024-10-25 22:22:29 瀏覽:875
hibernate使用二級緩存 發布:2024-10-25 22:14:00 瀏覽:359
手機版登陸布穀鳥伺服器地址 發布:2024-10-25 22:13:59 瀏覽:173
域名表示一台列印機伺服器的ip地址 發布:2024-10-25 22:13:05 瀏覽:981
簡歷php項目 發布:2024-10-25 22:06:15 瀏覽:707
鐵塔基站配置箱里有什麼 發布:2024-10-25 22:05:03 瀏覽:537
酷我上傳歌單 發布:2024-10-25 22:03:47 瀏覽:844
我的世界啟動器的伺服器如何加材質包 發布:2024-10-25 21:59:03 瀏覽:628