1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package com.google.archivepatcher.shared; 16 17import java.io.ByteArrayInputStream; 18import java.io.ByteArrayOutputStream; 19import java.io.IOException; 20import java.io.UnsupportedEncodingException; 21 22/** 23 * Data for one entry in the zip returned by {@link UnitTestZipArchive#makeTestZip()}. 24 */ 25public class UnitTestZipEntry { 26 /** 27 * The path under which the data is located in the archive. 28 */ 29 public final String path; 30 31 /** 32 * The compression level of the entry. 33 */ 34 public final int level; 35 36 /** 37 * The binary content of the entry. 38 */ 39 public final String content; 40 41 /** 42 * Optional comment, as an ASCII string. 43 */ 44 public final String comment; 45 46 /** 47 * Whether or not to use nowrap. 48 */ 49 public final boolean nowrap; 50 51 /** 52 * Creates a new entry with nowrap=true. 53 * @param path the path under which the data is located in the archive 54 * @param level the compression level of the entry 55 * @param content the binary content of the entry, as an ASCII string 56 * @param comment optional comment, as an ASCII string 57 */ 58 public UnitTestZipEntry(String path, int level, String content, String comment) { 59 this(path, level, true, content, comment); 60 } 61 62 /** 63 * Creates a new entry. 64 * @param path the path under which the data is located in the archive 65 * @param level the compression level of the entry 66 * @param content the binary content of the entry, as an ASCII string 67 * @param comment optional comment, as an ASCII string 68 */ 69 public UnitTestZipEntry(String path, int level, boolean nowrap, String content, String comment) { 70 this.path = path; 71 this.level = level; 72 this.nowrap = nowrap; 73 this.content = content; 74 this.comment = comment; 75 } 76 77 /** 78 * Returns the uncompressed content of the entry as a byte array for unit test simplicity. If the 79 * level is 0, this is the same as the actual array of bytes that will be present in the zip 80 * archive. If the level is not 0, this is the result of uncompressed the bytes that are present 81 * in the zip archive for this entry. 82 * @return as described 83 */ 84 public byte[] getUncompressedBinaryContent() { 85 try { 86 return content.getBytes("US-ASCII"); 87 } catch (UnsupportedEncodingException e) { 88 throw new RuntimeException("System doesn't support US-ASCII"); // Not likely 89 } 90 } 91 92 /** 93 * Returns the compressed form of the content, according to the level, that should be found in the 94 * zip archive. If the level is 0 (store, i.e. not compressed) this is the same as calling 95 * {@link #getUncompressedBinaryContent()}. 96 * @return the content, as a byte array 97 */ 98 public byte[] getCompressedBinaryContent() { 99 if (level == 0) { 100 return getUncompressedBinaryContent(); 101 } 102 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 103 DeflateCompressor compressor = new DeflateCompressor(); 104 compressor.setCompressionLevel(level); 105 compressor.setNowrap(nowrap); 106 try { 107 compressor.compress(new ByteArrayInputStream(getUncompressedBinaryContent()), buffer); 108 } catch (IOException e) { 109 throw new RuntimeException(e); // Shouldn't happen as this is all in-memory 110 } 111 return buffer.toByteArray(); 112 } 113} 114