开发者社区> 问答> 正文

java读取zip压缩文件里面二级文件夹目录内的文件的问题

我想在不解压的条件下得到一个压缩文件夹内部的二级目录下的文件流,但是搜了好久也没找到
代码如下

public class X509Util {
public static X509Certificate readSignatureBlock(InputStream in) throws IOException, GeneralSecurityException {

PKCS7 pkcs7 = new PKCS7(in);

return pkcs7.getCertificates()[0];

}

public static void print_key(String addr) throws FileNotFoundException, IOException, GeneralSecurityException {

//X509Certificate publicKey = readSignatureBlock(new FileInputStream(addr)); 
X509Certificate publicKey = readSignatureBlock(getX509InputStream(addr));
System.out.println("issuer:" + publicKey.getIssuerDN());

System.out.println("subject:" + publicKey.getSubjectDN());

System.out.println(publicKey.getPublicKey());

}
private static InputStream getX509InputStream(String apkPath) {
InputStream inputStream = null;
//InputStream xmlInputStream = null;
FileInputStream fstream = null;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(apkPath);
//ZipEntry zipEntry = new ZipEntry("\META-INF\CERT.RSA");//就是这里的问题,我想要读取META-INF文件夹里的CERT.RSA文件,但是这样无法读取到
ZipEntry zipEntry = new ZipEntry("META-INF/");
inputStream = zipFile.getInputStream(zipEntry);
} catch (IOException e) {
e.printStackTrace();
try {
inputStream.close();
fstream.close();
zipFile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return inputStream;
}
}

展开
收起
蛮大人123 2016-06-01 15:06:55 5909 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    public static void main(String[] args) {
            try {
                Enumeration<URL> urls = X509Util.class.getClassLoader().getResources("META-INF/services/java.nio.file.spi.FileSystemProvider");
                // 你的应该写成 "META-INF/CERT.RSA",注意META前不要有'/'
                while (urls.hasMoreElements()) {
                    URL url = urls.nextElement();
                    InputStream in = url.openStream();
    
                    BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
                    System.out.println(br.readLine());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    2019-07-17 19:23:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载