程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 解壓-java讀取zip壓縮文件裡面二級文件夾目錄內的文件的問題

解壓-java讀取zip壓縮文件裡面二級文件夾目錄內的文件的問題

編輯:編程解疑
java讀取zip壓縮文件裡面二級文件夾目錄內的文件的問題

我想在不解壓的條件下得到一個壓縮文件夾內部的二級目錄下的文件流,但是搜了好久也沒找到
代碼如下

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import sun.security.pkcs.PKCS7;

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;
}
}

最佳回答:


 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();
        }
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved