File compression and decompression, File compression and decompression

Source: Internet
Author: User

File compression and decompression, File compression and decompression

// File compression and decompression
Public class GzipUtils {

public static void zip(String sourcePath, String outPath) throws Exception {    zip(new File(sourcePath), new File(outPath));}public static void zip(File source, File out) throws Exception {    zip(new FileInputStream(source), new FileOutputStream(out));}public static void zip(InputStream is, OutputStream os) throws Exception {    GZIPOutputStream gos = null;    try {        gos = new GZIPOutputStream(os);        int len;        byte[] buffer = new byte[1024];        while ((len = is.read(buffer)) != -1) {            gos.write(buffer, 0, len);        }    } finally {        StreamUtils.closeStream(gos);        StreamUtils.closeStream(os);        StreamUtils.closeStream(is);    }}public static void unZip(String zipPath, String unzipPath)        throws Exception {    unZip(new File(zipPath), new File(unzipPath));}public static void unZip(File zipFile, File unzipFile) throws Exception {    unZip(new FileInputStream(zipFile), new FileOutputStream(unzipFile));}public static void unZip(InputStream is, OutputStream os) throws Exception {    GZIPInputStream gis = null;    try {        gis = new GZIPInputStream(is);        int len;        byte[] buffer = new byte[1024];        while ((len = gis.read(buffer)) != -1) {            os.write(buffer, 0, len);        }    } finally {        StreamUtils.closeStream(os);        StreamUtils.closeStream(gis);        StreamUtils.closeStream(is);    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.