壓縮與解壓縮

核心接口

Compressor

package com.xxx.arch.mw.nbp.common.csp;import com.xxx.arch.mw.nbp.common.constant.CommonConstants;
import com.xxx.arch.mw.nbp.common.exception.NbpException;import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.ZSTD;/*** */
public interface Compressor {int BUFFER_LENGTH = CommonConstants.DEFAULT_COMPRESSION_BUFFER_SIZE;CompressorEnum DEFAULT_COMPRESSOR_ALGORITHM = CompressorEnum.toEnumFromName(CommonConstants.DEFAULT_COMPRESSION_ALGORITHM);/*** compress data** @param data* @return* @throws NbpException*/byte[] compress(byte[] data) throws NbpException;
}

Decompressor

package com.xxx.arch.mw.nbp.common.csp;import com.xxx.arch.mw.nbp.common.constant.CommonConstants;
import com.xxx.arch.mw.nbp.common.exception.NbpException;/*** */
public interface Decompressor {int BUFFER_LENGTH = CommonConstants.DEFAULT_COMPRESSION_BUFFER_SIZE;/*** dcompress data* @param data* @return* @throws NbpException*/byte[] decompress(byte[] data) throws NbpException;
}

關鍵實現

CompressorEnum

package com.xxx.arch.mw.nbp.common.csp;import lombok.Getter;/*** */
public enum CompressorEnum {REVERSED(0, "reversed", "僅反轉"),BZIP2(1, "bzip2", "BZIP2算法,壓縮比高但耗時耗CPU"),GZIP(2, "gz", "GZIP算法,壓縮比高但耗時耗CPU"),SNAPPY_FRAMED(3, "snappy-framed", "SNAPPY_FRAMED算法"),DEFLATE(4, "deflate", "DEFLATE算法"),PACK200(5, "pack200", "DEFLATE64算法"),LZ4_BLOCK(6, "lz4-block", "LZ4_BLOCK算法"),LZ4_FRAMED(7, "lz4-framed", "LZ4_FRAMED算法"),ZSTD(8, "zstd", "ZSTD算法");@Getterprivate int id;@Getterprivate String name;@Getterprivate String desc;CompressorEnum(int id, String name, String desc) {this.id = id;this.name = name;this.desc = desc;}public static CompressorEnum toEnumFromId(int id) {for (CompressorEnum eum : values()) {if (eum.getId() == id) {return eum;}}return null;}public static CompressorEnum toEnumFromName(String name) {if (name == null || name.length() == 0) {return null;}for (CompressorEnum eum : values()) {if (eum.getName().equalsIgnoreCase(name)) {return eum;}}return null;}
}

AbstractCompressor

package com.xxx.arch.mw.nbp.common.csp;import com.xxx.arch.mw.nbp.common.domain.NbpCode;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
import org.apache.commons.compress.compressors.CompressorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.*;/*** */
public abstract class AbstractCompressor implements Compressor {private final Logger logger = LoggerFactory.getLogger(this.getClass());@Overridepublic byte[] compress(byte[] data) throws NbpException {try (ByteArrayInputStream is = new ByteArrayInputStream(data);ByteArrayOutputStream os = new ByteArrayOutputStream()) {compress(is, os);return os.toByteArray();} catch (IOException | CompressorException e) {logger.error("NBP-CLIENT UTIL", "compress data error with reason:", e);throw new NbpException(NbpCode.COMPRESSION_ERROR.getCode(), e.getMessage(), e.getCause());}}public abstract void compress(InputStream is, OutputStream os) throws IOException, CompressorException;
}

AbstractDecompressor

package com.xxx.arch.mw.nbp.common.csp;import com.xxx.arch.mw.nbp.common.domain.NbpCode;
import com.xxx.arch.mw.nbp.common.exception.NbpException;
import org.apache.commons.compress.compressors.CompressorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.*;/*** */
public abstract class AbstractDecompressor implements Decompressor {private final Logger logger = LoggerFactory.getLogger(this.getClass());@Overridepublic byte[] decompress(byte[] data) throws NbpException {try (ByteArrayInputStream is = new ByteArrayInputStream(data);ByteArrayOutputStream os = new ByteArrayOutputStream()) {decompress(is, os);return os.toByteArray();} catch (IOException | CompressorException e) {logger.error("NBP-CLIENT UTIL", "decompress data error with reason:", e);throw new NbpException(NbpCode.COMPRESSION_ERROR.getCode(), e.getMessage(), e.getCause());}}public abstract void decompress(InputStream is, OutputStream os) throws IOException, CompressorException;}

CompressorFactory

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp;import com.xxx.arch.mw.nbp.common.csp.bzip2.Bzip2Compressor;
import com.xxx.arch.mw.nbp.common.csp.deflate.DeflateCompressor;
import com.xxx.arch.mw.nbp.common.csp.gzip.GzipCompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4block.Lz4BlockCompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4framed.Lz4FramedCompressor;
import com.xxx.arch.mw.nbp.common.csp.pack200.Pack200Compressor;
import com.xxx.arch.mw.nbp.common.csp.reversed.ReversedCompressor;
import com.xxx.arch.mw.nbp.common.csp.snappyframed.SnappyFramedCompressor;
import com.xxx.arch.mw.nbp.common.csp.zstd.ZstdCompressor;import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.ZSTD;/*** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-14 11:20*/
public class CompressorFactory {private static final ConcurrentMap<CompressorEnum, Compressor> COMPRESSOR_MAP = new ConcurrentHashMap<>();public static Compressor getDefaultCompressor() {return getCompressor(Compressor.DEFAULT_COMPRESSOR_ALGORITHM);}public static Compressor getCompressor(CompressorEnum compressorEnum) {Compressor compressor = COMPRESSOR_MAP.get(compressorEnum);if (compressor == null) {switch (compressorEnum) {case REVERSED:compressor = new ReversedCompressor();break;case BZIP2:compressor = new Bzip2Compressor();break;case GZIP:compressor = new GzipCompressor();break;case SNAPPY_FRAMED:compressor = new SnappyFramedCompressor();break;case DEFLATE:compressor = new DeflateCompressor();break;case PACK200:compressor = new Pack200Compressor();break;case LZ4_BLOCK:compressor = new Lz4BlockCompressor();break;case LZ4_FRAMED:compressor = new Lz4FramedCompressor();break;case ZSTD:compressor = new ZstdCompressor();break;default:}if (compressor != null) {COMPRESSOR_MAP.putIfAbsent(compressorEnum, compressor);}}return compressor;}}

DecompressorFactory

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp;import com.xxx.arch.mw.nbp.common.csp.bzip2.Bzip2Decompressor;
import com.xxx.arch.mw.nbp.common.csp.deflate.DeflateDecompressor;
import com.xxx.arch.mw.nbp.common.csp.gzip.GzipDecompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4block.Lz4BlockDecompressor;
import com.xxx.arch.mw.nbp.common.csp.lz4framed.Lz4FramedDecompressor;
import com.xxx.arch.mw.nbp.common.csp.pack200.Pack200Decompressor;
import com.xxx.arch.mw.nbp.common.csp.reversed.ReversedDecompressor;
import com.xxx.arch.mw.nbp.common.csp.snappyframed.SnappyFramedDecompressor;
import com.xxx.arch.mw.nbp.common.csp.zstd.ZstdDecompressor;import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.DEFLATE;
import static com.xxx.arch.mw.nbp.common.csp.CompressorEnum.ZSTD;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-14 11:20*/
public class DecompressorFactory {private static final ConcurrentMap<CompressorEnum, Decompressor> DECOMPRESSOR_MAP = new ConcurrentHashMap<>();public static Decompressor getDefaultDecompressor() {return getDecompressor(Compressor.DEFAULT_COMPRESSOR_ALGORITHM);}public static Decompressor getDecompressor(CompressorEnum compressorEnum) {Decompressor decompressor = DECOMPRESSOR_MAP.get(compressorEnum);if (decompressor == null) {switch (compressorEnum) {case REVERSED:decompressor = new ReversedDecompressor();break;case BZIP2:decompressor = new Bzip2Decompressor();break;case GZIP:decompressor = new GzipDecompressor();break;case SNAPPY_FRAMED:decompressor = new SnappyFramedDecompressor();break;case DEFLATE:decompressor = new DeflateDecompressor();break;case PACK200:decompressor = new Pack200Decompressor();break;case LZ4_BLOCK:decompressor = new Lz4BlockDecompressor();break;case LZ4_FRAMED:decompressor = new Lz4FramedDecompressor();break;case ZSTD:decompressor = new ZstdDecompressor();break;default:}if (decompressor != null) {DECOMPRESSOR_MAP.putIfAbsent(compressorEnum, decompressor);}}return decompressor;}}

算法實現

ZSTD

ZstdCompressor

package com.xxx.arch.mw.nbp.common.csp.zstd;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import com.github.luben.zstd.ZstdOutputStream;
import org.apache.commons.compress.compressors.CompressorException;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** @date 2023/06/25*/
public class ZstdCompressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (ZstdOutputStream zos = new ZstdOutputStream(os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {zos.write(buffer, 0, pos);}}}
}

ZstdDecompressor

package com.xxx.arch.mw.nbp.common.csp.zstd;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import com.github.luben.zstd.ZstdInputStream;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** @date 2023/06/25*/
public class ZstdDecompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (ZstdInputStream zis = new ZstdInputStream(is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = zis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}
}

snappyframed

SnappyFramedCompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.snappyframed;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class SnappyFramedCompressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.SNAPPY_FRAMED, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

SnappyFramedDecompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.snappyframed;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class SnappyFramedDecompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.SNAPPY_FRAMED, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

reversed

ReversedCompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.reversed;import com.xxx.arch.mw.nbp.common.csp.Compressor;
import com.xxx.arch.mw.nbp.common.exception.NbpException;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class ReversedCompressor implements Compressor {@Overridepublic byte[] compress(byte[] data) throws NbpException {int length = data.length;byte[] compressed = new byte[length];for (int i = 0; i < length; i++) {compressed[i] = data[length - 1 - i];}return compressed;}
}

ReversedDecompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.reversed;import com.xxx.arch.mw.nbp.common.csp.Decompressor;
import com.xxx.arch.mw.nbp.common.exception.NbpException;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class ReversedDecompressor implements Decompressor {@Overridepublic byte[] decompress(byte[] data) throws NbpException {int length = data.length;byte[] decompressed = new byte[length];for (int i = 0; i < length; i++) {decompressed[i] = data[length - 1 - i];}return decompressed;}}

pack200

Pack200Compressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.pack200;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Pack200Compressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.PACK200, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

Pack200Decompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.pack200;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Pack200Decompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.PACK200, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

lz4framed

Lz4FramedCompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.lz4framed;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Lz4FramedCompressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.LZ4_FRAMED, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

Lz4FramedDecompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.lz4framed;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Lz4FramedDecompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.LZ4_FRAMED, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

lz4block

Lz4BlockCompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.lz4block;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Lz4BlockCompressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.LZ4_BLOCK, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

Lz4BlockDecompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.lz4block;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Lz4BlockDecompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.LZ4_BLOCK, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

gzip

GzipCompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.gzip;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class GzipCompressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

GzipDecompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.gzip;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class GzipDecompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.GZIP, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

deflate

DeflateCompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.deflate;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class DeflateCompressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.DEFLATE, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

DeflateDecompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.deflate;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class DeflateDecompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.DEFLATE, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

bzip2

Bzip2Compressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.bzip2;import com.xxx.arch.mw.nbp.common.csp.AbstractCompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Bzip2Compressor extends AbstractCompressor {@Overridepublic void compress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.BZIP2, os)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = is.read(buffer, 0, buffer.length))) {cos.write(buffer, 0, pos);}}}
}

Bzip2Decompressor

/** Copyright 2017-2018 Iabc Co.Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.xxx.arch.mw.nbp.common.csp.bzip2;import com.xxx.arch.mw.nbp.common.csp.AbstractDecompressor;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** Project: remote-repository** @author <a href="mailto:h@iabc.io">shuchen</a>* @version V1.0.0* @since 2020-07-13 09:53*/
public class Bzip2Decompressor extends AbstractDecompressor {@Overridepublic void decompress(InputStream is, OutputStream os) throws IOException, CompressorException {try (CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2, is)) {int pos;byte[] buffer = new byte[BUFFER_LENGTH];while (-1 != (pos = cis.read(buffer, 0, buffer.length))) {os.write(buffer, 0, pos);}}}}

工具使用

壓縮

 final CompressorEnum compressorEnum = CompressorEnum.toEnumFromName(propertyDTO.getPublisher().getCompressAlgorithm());final Compressor compressor = compressorEnum != null ?CompressorFactory.getCompressor(compressorEnum) : CompressorFactory.getDefaultCompressor();byte[] body = ConverterUtil.toBody(singleDetail.getUserContext());final byte[] compressedBody = compressor.compress(body);final String compressedContext = Base64.getEncoder().encodeToString(compressedBody);

解壓縮

final CompressorEnum compressorEnum = CompressorEnum.toEnumFromName(singleDetail.getUserContext().get(InstanceKey.COMPRESSED_ALGORITHM_KEY));final Decompressor decompressor = compressorEnum != null ?DecompressorFactory.getDecompressor(compressorEnum) : DecompressorFactory.getDefaultDecompressor();final byte[] body = decompressor.decompress(compressedBody);

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/166373.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/166373.shtml
英文地址,請注明出處:http://en.pswp.cn/news/166373.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【mybatis】使用<foreach>報錯parameter ‘id‘ not found

程序其他sql都執行正常&#xff0c;也寫了param注解&#xff0c;但是還是一直報parameter ‘id’ not found。最后發現是調用sql的實現類里ids的入參對象名稱不叫ids&#xff0c;叫idList 代碼如下&#xff1a; List<Map<String,String>> resultmapper.sql(date,…

【攻防世界-misc】pure_color

1.方法一&#xff1a;用畫圖工具打開圖片&#xff0c;將圖片拷貝至虛擬機win7桌面&#xff0c; 點“屬性”&#xff0c;顏色設置為“黑白”&#xff0c; 出現flag值。 2.方法二&#xff1a;使用Stegsilve打開&#xff0c;分析圖片 將圖片打開&#xff0c;按左右鍵查找&#xff…

c#數據庫:vs2022 加入mysql數據源

網上有VS2019連接MySQL數據庫的&#xff0c;那么VS2022&#xff0c;VS2023如果和連接到mysql數據庫呢&#xff0c;這里總結一下我的經歷&#xff1a; 1、首先下載ODBC驅動安裝包 當前下載地址&#xff1a;https://dev.mysql.com/downloads/connector/odbc/ 2、ODBC安裝 下載完…

openGauss學習筆記-131 openGauss 數據庫運維-啟停openGauss

文章目錄 openGauss學習筆記-131 openGauss 數據庫運維-啟停openGauss131.1 啟動openGauss131.2 停止openGauss131.3 示例131.3.1 啟動openGauss131.3.2 停止openGauss 131.4 錯誤排查 openGauss學習筆記-131 openGauss 數據庫運維-啟停openGauss 131.1 啟動openGauss 以操作系…

【ChatGLM3-6B】Docker下快速部署

【ChatGLM2-6B】小白入門及Docker下部署 前提下載安裝包網盤地址 開始安裝加載鏡像啟動鏡像進入容器啟動模型交互頁面訪問頁面地址 前提 安裝好了docker安裝好了NVIDIA顯卡16G 下載安裝包 網盤地址 ? 這里因為網盤上傳文件有大小限制&#xff0c;所以使用了分卷壓縮的方式…

【深度學習】CNN中pooling層的作用

1、pooling是在卷積網絡&#xff08;CNN&#xff09;中一般在卷積層&#xff08;conv&#xff09;之后使用的特征提取層&#xff0c;使用pooling技術將卷積層后得到的小鄰域內的特征點整合得到新的特征。一方面防止無用參數增加時間復雜度&#xff0c;一方面增加了特征的整合度…

MySql數據庫常用指令(一)

MySql數據庫常用指令&#xff08;一&#xff09; 一、MySQL 創建數據庫二、MySQL 刪除數據庫三、選擇數據庫四、選擇數據庫五、創建數據表六、刪除數據表七、插入數據八、查詢數據 注&#xff1a;文中TEST為測試所用數據庫&#xff0c;根據實際應用修改 一、MySQL 創建數據庫 …

JVM中如何實現垃圾收集

Java虛擬機&#xff08;JVM&#xff09;使用垃圾收集器&#xff08;Garbage Collector&#xff09;來管理內存&#xff0c;清理不再使用的對象以釋放內存空間。垃圾收集的主要目標是自動化內存管理&#xff0c;使開發人員無需顯式地釋放不再使用的內存&#xff0c;從而降低了內…

Mysql面經

Select語句的執行順序 1、from 子句組裝來自不同數據源的數據&#xff1b; 2、where 子句基于指定的條件對記錄行進行篩選&#xff1b; 3、group by 子句將數據劃分為多個分組&#xff1b; 4、使用聚集函數進行計算&#xff1b;AVG() SUM() MAX() MIN() COUNT() 5、使用 havin…

Vue模板引用

Vue的模板引用是為了處理直接訪問DOM底層而做的補充處理&#xff0c;畢竟Vue宣稱是基于組件的&#xff0c;這種補充處理是對Vue框架的補充。在前端基于BOMDOMjs的組成來看&#xff0c;Vue保留模板引用是留下了一種框架設計的余裕。 模板引用案例如下&#xff1a; <script s…

2016年8月18日 Go生態洞察:Go 1.7版本二進制文件縮小

&#x1f337;&#x1f341; 博主貓頭虎&#xff08;&#x1f405;&#x1f43e;&#xff09;帶您 Go to New World?&#x1f341; &#x1f984; 博客首頁——&#x1f405;&#x1f43e;貓頭虎的博客&#x1f390; &#x1f433; 《面試題大全專欄》 &#x1f995; 文章圖文…

【經驗分享】在vm中安裝openEuler及使用yum安裝openGauss

1.前言 隨著互聯網時代對數據庫的新要求,以PostgreSQL為基礎的開源數據庫openGauss應運而生。openGauss在保持PostgreSQL接口兼容的前提下,對其查詢優化器、高可用特性等進行了全面優化,實現了超高性能。 同時,openGauss作為社區項目,新增功能持續豐富。優點是查詢性能高、可…

機器學習——詞向量模型(CBOW代碼實現-未開始)

本來是不打算做這個CBOW代碼案例的&#xff0c;想快馬加鞭看看前饋神經網絡 畢竟書都買好了 可是…可是…我看書的時候&#xff0c;感覺有點兒困難&#xff0c;哭的很大聲… 感覺自己腦細胞可能無法這么快接受 要不&#xff0c;還是退而求個稍微難度沒那么大的事&#xff0c;想…

【多線程】-- 01 線程創建之繼承Thread多線程同步下載網絡圖片

多線程 1 簡介 1.1 多任務、多線程 普通方法調用&#xff1a;只有主線程一條執行路徑 多線程&#xff1a;多條執行路徑&#xff0c;主線程和子線程并行交替執行 如下圖所示&#xff1a; 1.2 程序.進程.線程 一個進程可以有多個線程&#xff0c;例如視頻中同時聽聲音、看圖…

idea 問題合集

調試按鈕失效&#xff1a; 依次點擊&#xff1a;Modules-web-src-Sources&#xff0c;重啟IDEA即可&#xff08;網上看到的方法&#xff0c;原因呢未明&#xff09;

U-boot(四):start_armboot

本文主要探討210的uboot啟動的第二階段&#xff0c;主要函數為start_armboot。 uboot 一階段初始化SoC內部部件(看門狗、時鐘等),初始化DDR,重定位 二階段初始化其余硬件(iNand、網卡芯片)以及命令、環境變量等 啟動打印硬件信息,進入bootdelay,讀秒完后執行bootc…

SpringCloud Alibaba集成 Gateway(自定義負載均衡器)、Nacos(配置中心、注冊中心)、loadbalancer

文章目錄 POM依賴環境準備配置配置文件配置類 案例展示 POM依賴 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.10</version><relativePath/></p…

【華為OD】C卷真題 100%通過:執行時長 C/C++實現

【華為OD】C卷真題 100%通過:執行時長 C/C實現 目錄 題目描述&#xff1a; 示例1 示例2 代碼實現&#xff1a; 題目描述&#xff1a; 為了充分發揮GPU算力&#xff0c;需要盡可能多的將任務交給GPU執行&#xff0c;現在有一個任務數組&#xff0c;數組元素表示在這1秒內…

百度ai試用

JMaven Central: com.baidu.aip:java-sdk (sonatype.com) Java sdk地址如上&#xff1a; 文心一言開發者 文心一言 (baidu.com) ERNIE Bot SDK提供便捷易用的接口&#xff0c;可以調用文心一言的能力&#xff0c;包含文本創作、通用對話、語義向量、AI作圖等。 pip install…

什么是輕量應用服務器?可以從亞馬遜云科技的優勢入手了解

什么是輕量應用服務器&#xff1f; 隨著如今各行各業對云計算的需求越來越多&#xff0c;云服務器也被越來越多的企業所廣泛采用。其中&#xff0c;輕量應用服務器是一種簡單、高效、可靠的云計算服務&#xff0c;能夠為開發人員、企業和個人提供輕量級的虛擬專用服務器&#x…