2 * Copyright (c) 2018-2019. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.qrcode.zip;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.util.zip.Deflater;
22 import java.util.zip.DeflaterOutputStream;
24 import org.hedgecode.chess.qrcode.ChessQRCodeConstants;
25 import org.hedgecode.chess.qrcode.ChessQRCodeException;
30 * @author Dmitry Samoshin aka gotty
32 public class ZipContentDeflater {
34 public static String deflate(String contents) throws ChessQRCodeException {
35 byte[] bytes = contents.getBytes(
36 ChessQRCodeConstants.CHARSET
38 if (bytes.length > ZipContentUtils.getMaxContentLength()) {
39 throw new ChessQRCodeException(
40 ChessQRCodeException.Type.PARSE, "zip.max.length.error"
43 ByteArrayOutputStream bout = new ByteArrayOutputStream();
44 DeflaterOutputStream zout = new DeflaterOutputStream(
45 bout, new Deflater(Deflater.BEST_COMPRESSION)
54 } catch (IOException e) {
55 throw new ChessQRCodeException(
56 ChessQRCodeException.Type.PARSE, "zip.deflate.write.error", e.getMessage()
59 return ZipContentUtils.base64Encode(
60 ZipContentUtils.concat(
68 public static void main(String[] args) throws ChessQRCodeException {
69 StringBuilder sb = new StringBuilder();
70 for (int i = 0; i < 10; ++i) {
71 sb.append(i).append("bla-bla-bla").append(i);
73 String zipContents = ZipContentDeflater.deflate(sb.toString());
74 System.out.println("Zip Contents: " + zipContents);
75 System.out.println("Zip length: " + zipContents.length());
76 String contents = ZipContentInflater.inflate(zipContents);
77 System.out.println("Unzip Contents: " + contents);
78 System.out.println("Unziip length: " + contents.length());
79 System.out.println("Equals: " + sb.toString().equals(contents));