集册 Java实例教程 压缩到字节

压缩到字节

欢马劈雪     最近更新时间:2020-01-02 10:19:05

389
压缩到字节


//package com.nowjava;// 来 自 N o  w  J a v a . c o m - 时  代  Java

import java.io.*;


import java.util.zip.ZipEntry;


import java.util.zip.ZipOutputStream;


public class Main {

    public static byte[] compressToBytes(String str) {

        if (str == null) {

            return null;

        }
/** n o w  j a v a  . c o m 提供 **/

        byte[] zipBytes = null;

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

        ZipOutputStream zipOut = null;


        try {

            zipOut = new ZipOutputStream(byteOut);

            zipOut.putNextEntry(new ZipEntry("zipEntry"));

            zipOut.write(str.getBytes());

            zipOut.closeEntry();

            zipBytes = byteOut.toByteArray();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (zipOut != null) {

                
展开阅读全文