[LIB-9] Add ability to archive qrcode content
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / zip / ZipContentDeflater.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.qrcode.zip;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.util.zip.Deflater;
22 import java.util.zip.DeflaterOutputStream;
23
24 import org.hedgecode.chess.qrcode.ChessQRCodeConstants;
25 import org.hedgecode.chess.qrcode.ChessQRCodeException;
26
27 /**
28  *
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class ZipContentDeflater {
33
34     public static String deflate(String contents) throws ChessQRCodeException {
35         byte[] bytes = contents.getBytes(
36                 ChessQRCodeConstants.CHARSET
37         );
38         if (bytes.length > ZipContentUtils.getMaxContentLength()) {
39             throw new ChessQRCodeException(
40                     ChessQRCodeException.Type.PARSE, "zip.max.length.error"
41             );
42         }
43         ByteArrayOutputStream bout = new ByteArrayOutputStream();
44         DeflaterOutputStream zout = new DeflaterOutputStream(
45                 bout, new Deflater(Deflater.BEST_COMPRESSION)
46         );
47         try {
48             try {
49                 zout.write(bytes);
50             } finally {
51                 zout.close();
52                 bout.close();
53             }
54         } catch (IOException e) {
55             throw new ChessQRCodeException(
56                     ChessQRCodeException.Type.PARSE, "zip.deflate.write.error", e.getMessage()
57             );
58         }
59         return ZipContentUtils.base64Encode(
60                 ZipContentUtils.concat(
61                         bytes.length,
62                         bout.toByteArray()
63                 )
64         );
65     }
66
67
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);
72         }
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));
80     }
81
82 }