Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Java program compresses and decompresses zip files

Shulou Source: shulou.com Published: 2022-06-03 05:54:39 10月11日 Update

Summary of knowledge you need to know:

DeflaterOutputStream: the base class of the compressed class.

ZipOutputStream: a subclass of DeflaterOutputStream that compresses data into Zip file format

GZIPOutputStream: a subclass of DeflaterOutputStream that compresses data into GZip file format

InflaterInputStream: the base class of the decompressed class

ZipInputStream: a subclass of InflaterInputStream that decompresses data in Zip format

GZIPInputStream: a subclass of InflaterInputStream that decompresses data in Zip format

ZipEntry class: represents an ZIP file entry

ZipFile class: this class is used to read entries from ZIP files

Name the list of files within the zip with zipentry.

Use FileInputStream to read the file.

Write compressed files in the zipoutputstream output stream.

The logic is simple and easy to understand, and the attachment has an example, which can be run after decompression.

Baidu network disk example link: https://pan.baidu.com/s/1VSs-UqtU_3Lld2H7-1dMMg

Extraction code: mcb3

Package com.crscd.server.web.cbtc2.filezip

Import java.io.BufferedInputStream

Import java.io.BufferedOutputStream

Import java.io.BufferedReader

Import java.io.File

Import java.io.FileInputStream

Import java.io.FileOutputStream

Import java.io.InputStreamReader

Public class FileZip {

/ *

Author:zhao_anan 20190424 zip compress program

/

/ public void zip (ZipOutputStream out, File f, String base, boolean first)

Throws Exception {

If (first) {

If (f.isDirectory ()) {

Out.putNextEntry (new ZipEntry (/))

Base = base + f.getName ()

First = false

} else

Base = f.getName ()

}

If (f.isDirectory ()) {

File [] fl = f.listFiles ()

Base = base + "/"

For (int I = 0; I < fl.length; iTunes +) {

Zip (out, fl [I], base + FLI] .getName (), first)

}

} else {

Out.putNextEntry (new ZipEntry (base))

FileInputStream in = new FileInputStream (f)

Int b

System.out.println (base)

While ((b = in.read ())! =-1) {

Out.write (b)

}

In.close ()

}

} * /

/ *

Unzip

/

/ public void unZipFileByOpache (ZipFile zipFile, String unZipRoot)

Throws Exception, IOException {

Enumeration e = zipFile.entries ()

ZipEntry zipEntry

While (e.hasMoreElements ()) {

ZipEntry = (ZipEntry) e.nextElement ()

InputStream fis = zipFile.getInputStream (zipEntry)

If (zipEntry.isDirectory ()) {

} else {

File file = new File (unZipRoot + File.separator

ZipEntry.getName ()

System.out.println (unZipRoot + File.separatorzipEntry.getName ())

File parentFile = file.getParentFile ()

ParentFile.mkdirs ()

FileOutputStream fos = new FileOutputStream (file)

Byte [] b = new byte [1024]

Int len

While (len = fis.read (b, 0, b.length))! =-1) {

Fos.write (b, 0, len)

}

Fos.close ()

Fis.close ()

}

}

}

Public void ZipFile (String zipFileName, String inputFileName)

Throws Exception {

ZipOutputStream out = new ZipOutputStream (new FileOutputStream (

ZipFileName))

File inputFile = new File (inputFileName)

Zip (out, inputFile, "", true)

System.out.println ("zip done")

Out.close ()

}

Public void unZipFile (String unZipFileName, String unZipPath)

Throws Exception {

/ / ZipFile zipFile = new ZipFile (unZipFileName)

/ / unZipFileByOpache (zipFile, unZipPath)

System.out.println ("unZip Ok")

}

Public static void main (String [] args) throws Exception {

FileZip fileZip = new FileZip ()

/ / ZipFile ("d:/testfile.zip", "d:/TTPlayer")

FileZip.unZipFile ("d:/20120206_SLIS_0002_DY2.zip", "dvl /")

} * /

/ * *

Unzip file 20190424@auther: zhao_anan@param unzipRoot

, /

Public void unZipFile (String zipFilename, String unzipRoot)

Throws Exception {

BufferedOutputStream bos = null

FileInputStream fis = new FileInputStream (zipFilename)

BufferedInputStream bis = new BufferedInputStream (fis)

ZipInputStream zis = new ZipInputStream (bis); ZipEntry entry = null;int count = 0 X byte buf [] = new byte [1024]; while ((entry = zis.getNextEntry ())! = null) {if (entry.isDirectory ()) {} else {File file = new File (unzipRoot + File.separator + entry.getName ()); File parentFile = file.getParentFile (); parentFile.mkdirs () Bos = new BufferedOutputStream (new FileOutputStream (file)); while ((count = zis.read (buf))! =-1) {bos.write (buf, 0, count);} bos.flush (); bos.close ();}} zis.close ()

}

/ * *

Zip@param zipFilename@param unzipRoot

, /

Private void zip (ZipOutputStream out, File f, String base, boolean first)

Throws Exception {

If (first) {

If (f.isDirectory ()) {

Out.putNextEntry (new ZipEntry (/))

Base = base + f.getName ()

First = false

} else

Base = f.getName ()

}

If (f.isDirectory ()) {

File [] fl = f.listFiles ()

Base = base + "/"

For (int I = 0; I < fl.length; iTunes +) {

Zip (out, fl [I], base + FLI] .getName (), first)

}

} else {

Out.putNextEntry (new ZipEntry (base))

/ / FileInputStream in = new FileInputStream (f)

BufferedReader in = new BufferedReader (new InputStreamReader (

New FileInputStream (f), "ISO8859_1"))

Int b

System.out.println (base)

While ((b = in.read ())! =-1) {

Out.write (b)

}

In.close ()

}

}

Public void zipFile (String zipFileName, String inputFileName)

Throws Exception {

ZipOutputStream out = new ZipOutputStream (new FileOutputStream (

ZipFileName))

File inputFile = new File (inputFileName)

Zip (out, inputFile, "", true)

System.out.println ("zip done")

Out.close ()

}

/ * *

Zip@param zipFilename@param unzipRoot

, /

Private void zipList (ZipOutputStream out, String [] fileList, String base)

Throws Exception {

Base = base + "\"

For (int I = 0; I < fileList.length; iTunes +) {

/ / zip (out, base + fileList [I], first)

Out.putNextEntry (new ZipEntry (fileList [I]))

BufferedReader in = new BufferedReader (new InputStreamReader (

New FileInputStream (base + fileList [I]), "ISO8859_1"))

Int b

System.out.println (base + fileList [I])

While ((b = in.read ())! =-1) {

Out.write (b)

}

In.close ()

}

}

Public void zipListFile (String zipFileName, String inputListFiles, String baseurl)

Throws Exception {

ZipOutputStream out = new ZipOutputStream (new FileOutputStream (

ZipFileName))

String [] FileList = inputListFiles.split (",")

ZipList (out, FileList, baseurl)

System.out.println ("zip done")

Out.close ()

}

Public static void main (String [] args) {

/ / Compression test FileZip tFileZip = new FileZip (); String tinputFileName = "E:\\ temp"; String baseurl = "E:\\ temp"; String tzipFileName = "E:\\ temp.zip"; String inputListFiles = "application-dev.yml,auto_arrange.yml"; try {/ / tFileZip.zipFile (tzipFileName, tinputFileName); tFileZip.zipListFile (tzipFileName, inputListFiles, baseurl); / / tFileZip.unZipFile (tzipFileName, baseurl + "unzip") } catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();}

}

}

Tags: Files subclasses data formats instances entries knowledge logic links attachments tests output runs programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Linux MariaDB Xiaomi