@@ -99,23 +99,28 @@ public static ByteBuffer getApkSigningBlock(File channelFile) throws ApkSignatur
9999 if (channelFile == null || !channelFile .exists () || !channelFile .isFile ()) {
100100 return null ;
101101 }
102- RandomAccessFile apk = new RandomAccessFile (channelFile , "r" );
103- //1.find the EOCD
104- Pair <ByteBuffer , Long > eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier .getEocd (apk );
105- ByteBuffer eocd = eocdAndOffsetInFile .getFirst ();
106- long eocdOffset = eocdAndOffsetInFile .getSecond ();
107-
108- if (ZipUtils .isZip64EndOfCentralDirectoryLocatorPresent (apk , eocdOffset )) {
109- throw new ApkSignatureSchemeV2Verifier .SignatureNotFoundException ("ZIP64 APK not supported" );
110- }
111-
112- //2.find the APK Signing Block. The block immediately precedes the Central Directory.
113- long centralDirOffset = ApkSignatureSchemeV2Verifier .getCentralDirOffset (eocd , eocdOffset );//通过eocd找到中央目录的偏移量
114- //3. find the apk V2 signature block
115- Pair <ByteBuffer , Long > apkSignatureBlock =
116- ApkSignatureSchemeV2Verifier .findApkSigningBlock (apk , centralDirOffset );//找到V2签名块的内容和偏移量
102+ RandomAccessFile apk = null ;
103+ try {
104+ apk = new RandomAccessFile (channelFile , "r" );
105+ //1.find the EOCD
106+ Pair <ByteBuffer , Long > eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier .getEocd (apk );
107+ ByteBuffer eocd = eocdAndOffsetInFile .getFirst ();
108+ long eocdOffset = eocdAndOffsetInFile .getSecond ();
109+
110+ if (ZipUtils .isZip64EndOfCentralDirectoryLocatorPresent (apk , eocdOffset )) {
111+ throw new ApkSignatureSchemeV2Verifier .SignatureNotFoundException ("ZIP64 APK not supported" );
112+ }
117113
118- return apkSignatureBlock .getFirst ();
114+ //2.find the APK Signing Block. The block immediately precedes the Central Directory.
115+ long centralDirOffset = ApkSignatureSchemeV2Verifier .getCentralDirOffset (eocd , eocdOffset );//通过eocd找到中央目录的偏移量
116+ //3. find the apk V2 signature block
117+ Pair <ByteBuffer , Long > apkSignatureBlock = ApkSignatureSchemeV2Verifier .findApkSigningBlock (apk , centralDirOffset );//找到V2签名块的内容和偏移量
118+ return apkSignatureBlock .getFirst ();
119+ }finally {
120+ if (apk != null ){
121+ apk .close ();
122+ }
123+ }
119124 }
120125
121126 /**
@@ -127,39 +132,46 @@ public static ByteBuffer getApkSigningBlock(File channelFile) throws ApkSignatur
127132 * @throws ApkSignatureSchemeV2Verifier.SignatureNotFoundException not have v2 sinature
128133 */
129134 public static ApkSectionInfo getApkSectionInfo (File baseApk , boolean lowMemory ) throws IOException , ApkSignatureSchemeV2Verifier .SignatureNotFoundException {
130- RandomAccessFile apk = new RandomAccessFile (baseApk , "r" );
131- //1.find the EOCD and offset
132- Pair <ByteBuffer , Long > eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier .getEocd (apk );
133- ByteBuffer eocd = eocdAndOffsetInFile .getFirst ();
134- long eocdOffset = eocdAndOffsetInFile .getSecond ();
135-
136- if (ZipUtils .isZip64EndOfCentralDirectoryLocatorPresent (apk , eocdOffset )) {
137- throw new ApkSignatureSchemeV2Verifier .SignatureNotFoundException ("ZIP64 APK not supported" );
138- }
135+ RandomAccessFile apk = null ;
136+ try {
137+ apk = new RandomAccessFile (baseApk , "r" );
138+ //1.find the EOCD and offset
139+ Pair <ByteBuffer , Long > eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier .getEocd (apk );
140+ ByteBuffer eocd = eocdAndOffsetInFile .getFirst ();
141+ long eocdOffset = eocdAndOffsetInFile .getSecond ();
142+
143+ if (ZipUtils .isZip64EndOfCentralDirectoryLocatorPresent (apk , eocdOffset )) {
144+ throw new ApkSignatureSchemeV2Verifier .SignatureNotFoundException ("ZIP64 APK not supported" );
145+ }
139146
140- //2.find the APK Signing Block. The block immediately precedes the Central Directory.
141- long centralDirOffset = ApkSignatureSchemeV2Verifier .getCentralDirOffset (eocd , eocdOffset );//通过eocd找到中央目录的偏移量
142- Pair <ByteBuffer , Long > apkSchemeV2Block = ApkSignatureSchemeV2Verifier .findApkSigningBlock (apk , centralDirOffset );//找到V2签名块的内容和偏移量
147+ //2.find the APK Signing Block. The block immediately precedes the Central Directory.
148+ long centralDirOffset = ApkSignatureSchemeV2Verifier .getCentralDirOffset (eocd , eocdOffset );//通过eocd找到中央目录的偏移量
149+ Pair <ByteBuffer , Long > apkSchemeV2Block = ApkSignatureSchemeV2Verifier .findApkSigningBlock (apk , centralDirOffset );//找到V2签名块的内容和偏移量
143150
144- //3.find the centralDir
145- Pair <ByteBuffer , Long > centralDir = findCentralDir (apk , centralDirOffset , (int ) (eocdOffset - centralDirOffset ));
151+ //3.find the centralDir
152+ Pair <ByteBuffer , Long > centralDir = findCentralDir (apk , centralDirOffset , (int ) (eocdOffset - centralDirOffset ));
146153
147- ApkSectionInfo apkSectionInfo = new ApkSectionInfo ();
148- apkSectionInfo .lowMemory = lowMemory ;
149- apkSectionInfo .apkSize = baseApk .length ();
150- if (!lowMemory ) {
151- //4.find the contentEntry
152- apkSectionInfo .contentEntry = findContentEntry (apk , (int ) apkSchemeV2Block .getSecond ().longValue ());
154+ ApkSectionInfo apkSectionInfo = new ApkSectionInfo ();
155+ apkSectionInfo .lowMemory = lowMemory ;
156+ apkSectionInfo .apkSize = baseApk .length ();
157+ if (!lowMemory ) {
158+ //4.find the contentEntry
159+ apkSectionInfo .contentEntry = findContentEntry (apk , (int ) apkSchemeV2Block .getSecond ().longValue ());
160+ }
161+ apkSectionInfo .schemeV2Block = apkSchemeV2Block ;
162+ apkSectionInfo .centralDir = centralDir ;
163+ apkSectionInfo .eocd = eocdAndOffsetInFile ;
164+
165+ //5. check Paramters
166+ apkSectionInfo .checkParamters ();
167+
168+ System .out .println ("baseApk : " + baseApk .getAbsolutePath () + "\n ApkSectionInfo = " + apkSectionInfo );
169+ return apkSectionInfo ;
170+ }finally {
171+ if (apk != null ){
172+ apk .close ();
173+ }
153174 }
154- apkSectionInfo .schemeV2Block = apkSchemeV2Block ;
155- apkSectionInfo .centralDir = centralDir ;
156- apkSectionInfo .eocd = eocdAndOffsetInFile ;
157-
158- //5. check Paramters
159- apkSectionInfo .checkParamters ();
160-
161- System .out .println ("baseApk : " + baseApk .getAbsolutePath () + "\n ApkSectionInfo = " + apkSectionInfo );
162- return apkSectionInfo ;
163175 }
164176
165177 /**
0 commit comments