1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 com.android.tools.build.apkzlib.zip;
18 
19 import javax.annotation.Nonnull;
20 
21 /**
22  * Information stored in the {@link CentralDirectoryHeader} that is related to compression and may
23  * need to be computed lazily.
24  */
25 public class CentralDirectoryHeaderCompressInfo {
26 
27     /**
28      * Version of zip file that only supports stored files.
29      */
30     public static final long VERSION_WITH_STORE_FILES_ONLY = 10L;
31 
32     /**
33      * Version of zip file that only supports directories and deflated files.
34      */
35     public static final long VERSION_WITH_DIRECTORIES_AND_DEFLATE = 20L;
36 
37     /**
38      * The compression method.
39      */
40     @Nonnull
41     private final CompressionMethod mMethod;
42 
43     /**
44      * Size of the file compressed. 0 if the file has no data.
45      */
46     private final long compressedSize;
47 
48     /**
49      * Version needed to extract the zip.
50      */
51     private final long versionExtract;
52 
53     /**
54      * Creates new compression information for the central directory header.
55      *
56      * @param method the compression method
57      * @param compressedSize the compressed size
58      * @param versionToExtract minimum version to extract (typically
59      * {@link #VERSION_WITH_STORE_FILES_ONLY} or {@link #VERSION_WITH_DIRECTORIES_AND_DEFLATE})
60      */
CentralDirectoryHeaderCompressInfo( @onnull CompressionMethod method, long compressedSize, long versionToExtract)61     public CentralDirectoryHeaderCompressInfo(
62             @Nonnull CompressionMethod method,
63             long compressedSize,
64             long versionToExtract) {
65         mMethod = method;
66         this.compressedSize = compressedSize;
67         versionExtract = versionToExtract;
68     }
69 
70     /**
71      * Creates new compression information for the central directory header.
72      *
73      * @param header the header this information relates to
74      * @param method the compression method
75      * @param compressedSize the compressed size
76      */
CentralDirectoryHeaderCompressInfo(@onnull CentralDirectoryHeader header, @Nonnull CompressionMethod method, long compressedSize)77     public CentralDirectoryHeaderCompressInfo(@Nonnull CentralDirectoryHeader header,
78             @Nonnull CompressionMethod method, long compressedSize) {
79         mMethod = method;
80         this.compressedSize = compressedSize;
81 
82         if (header.getName().endsWith("/") || method == CompressionMethod.DEFLATE) {
83             /*
84              * Directories and compressed files only in version 2.0.
85              */
86             versionExtract = VERSION_WITH_DIRECTORIES_AND_DEFLATE;
87         } else {
88             versionExtract = VERSION_WITH_STORE_FILES_ONLY;
89         }
90     }
91 
92     /**
93      * Obtains the compression data size.
94      *
95      * @return the compressed data size
96      */
getCompressedSize()97     public long getCompressedSize() {
98         return compressedSize;
99     }
100 
101     /**
102      * Obtains the compression method.
103      *
104      * @return the compression method
105      */
106     @Nonnull
getMethod()107     public CompressionMethod getMethod() {
108         return mMethod;
109     }
110 
111     /**
112      * Obtains the minimum version for extract.
113      *
114      * @return the minimum version
115      */
getVersionExtract()116     public long getVersionExtract() {
117         return versionExtract;
118     }
119 }
120