Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Liệt kê tất cả các tập tin có trong 1 file JAR hoặc ZIP

Để liệt kê tất cả các file được nén trong 1 file zip hoặc jar, trước hết ta dùng đối tượng java.util.jar.JarFile để đọc file này, sau đó dùng đối tượng java.util.jar.JarEntry để lấy từng entry có trong jar/zip file này. Code hoàn chình như sau:

import java.util.Enumeration;

import java.util.Scanner;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

public class ReadJARFile {

public static void main(String[] args) {

try {

System.out.println(“Nhập và tập tin jar/zip cần đọc.”+

“\nví dụ: C://abc//tyteo.zip”);

System.out.print(“File: “);

String filename=new Scanner(System.in).nextLine();

JarFile jar=new JarFile(filename);

Enumeration<JarEntry> list=jar.entries();

while(list.hasMoreElements())

{

JarEntry entry=list.nextElement();

System.out.println(entry);

}

} catch (Exception e) {

System.out.println(“Cannot reading source!\n”+

e.toString());

}

}

}

12 Responses to “Liệt kê tất cả các tập tin có trong 1 file JAR hoặc ZIP”

  1. sao không chạy được said

    Kính chào anh Hải!
    Mình cám ơn bài viết của anh rất nhiều, nhưng khi mình viết đoạn code của anh vào NetBean 6.7.1 thì không chạy được. Rất mong anh Hải giải thích và tư vấn thêm. Cám ơn anh rất nhiều.

  2. vovanhai said

    Chạy ngon lành mà. Bạn thử lại xem!

  3. VietHung said

    Dear Thay , code nay chi doc duoc file.zip khong doc duoc file.rar(bao loi : java.util.zip.ZipException : error in opening zip file)

  4. NguyenQuyen said

    Chào thầy!
    Mình là người mới tìm hiểu java, nhưng cái code của mình cần làm thì được viết trên linux. Mình đọc lý thuyết thì thấy rằng đều có thể chạy java ở trên môi trường hệ ĐH khác nhau.
    Mình phân biệt cái code của mình viết trên linux vì có 1 số file .sh.
    Như vậy mình muốn chuyển sang java trên win thì có cách nào k thầy?
    Vì mình cũng đã làm theo hướng dẫn của tắc giả trên môi trường ubuntun nhưng lại thấy báo lỗii k nhận được máy ảo. Mình thật sự không biết giải quyết như thế nào 😦
    Thầy có thể hướng dẫn giúp mình hay chỉ cho mình hướng đi/tài liệu để build được k?
    cám ơn thầy nhiều!

  5. Võ Văn Hải said

    cái file .sh chẳng qua là file .bat bên win(để thực thi lệnh DOS theo lô). Code java viết 1 lần chạy mọi nơi (WORA) mà. Copy code về paste vào 1 IDE nào đó(Eclipse chẳng hạn) thế là chạy thôi.

  6. Quyên Nguyễn said

    mình hiểu rồi. cám ơn thầy nhiều!
    mà mình cũng mới phát hiện ra toàn là file .class, k biết mình tìm được tool để dịch ngược lại thì có ổn k nhỉ?
    Thầy có kinh nghiệm gì trong vấn đề này k?
    cám ơn thầy đã rất nhiệt tình…

  7. NguyenQuyen said

    cám ơn thầy nhiều! mình đã coi được code của chương trình và hiểu được chút rồi….
    còn nữa, sao khi mình chạy file .jar thì thấy báo lỗi “failed to load main-class manifest attribute from…” trong khi cái gói của mình toàn là những file đã được biên dịch rồi. thầy có thể chỉ giúp mình được k?
    p/s: vì mình ra trường cũng được gần 10 năm rùi, giờ mới mày mò để học java. vậy nên rất nhiều thứ còn k biết..cám ơn thầy đã rất nhiệt tình!

  8. NguyenQuyen said

    mà mình thấy file .jar cảu mình là file thực thi mà..hjc..hjc..k biết tại sao nữa? thầy coi giúp mình đi 🙂

  9. Võ Văn Hải said

    Tùy loại jar mà có thể chạy hay không. Loại chạy được là loại executable jar file (file jar có lớp chứa hàm main được khai báo trong file manifest).

  10. NguyenQuyen said

    Thầy cho mình hỏi, trong 1 cái file .sh của mình có câu này, mình tìm hiểu thì nó yêu cầu chạy trên máy 64 bit, có đúng k thầy?
    JAVA=”java -XX:ThreadStackSize=256k -XX:+UseCompressedOops -XX:+UseParallelGC”

    như vậy, nếu mình có các file .java thì mình có phải chạy trên máy 64 bit hay k?
    cám ơn thầy nhiều!

  11. Võ Văn Hải said

    Code java không có khái niệm 32/64 bits. JRE mới có khái niệm này mà thôi (tức là môi trường thực thi). Code của JAVA là Write One RUn Anywhere mà.

  12. NguyenQuyen said

    như vậy mình phải cài HĐH 64bit mới chạy được phải k thầy?
    Khi mình bỏ tùy chọn UseCompressedOops đi thì báo lỗi không tạo được máy ảo. Mình post cái fie hướng dẫn lên, thầy coi giúp mình nhé!:
    — GNAT installation —
    ———————–

    1) Download several files
    2) Create and fill a relational database
    3) Edit property files
    4) Run a test

    1) Download several files
    ————————-

    Download the following files from Sourceforge into a new folder:
    – gnat.tar.gz – GNAT binaries for client and server, configurations,
    data, scripts, test cases needed for GNAT
    – dictionaries.tar.gz – example dictionaries for human, mouse, and fruit fly,
    genes; and for GeneOntology and MeSH term recognition
    – sql.tar.gz – gene information for GNAT

    Unzip all *.gz files into the same folder. The following sub-directories
    will be created:
    – config/ – various configuration files
    – data/ – static data required to run GNAT
    – dictionaries/ – data for NER dictionaries
    – documentation/ – readme files
    – lib/ – required libraries such as gnat.jar; you can store other
    libraries such as mysql.jar (see below) here as well.
    * if this directory is not present, create it and move
    gnat.jar and other libraries here
    – scripts/ – example scripts to generate and start dictionaries, run
    GNAT, etc.
    * you might need to change the class path in these scripts
    – sql/ – data that need to be loaded into a DB, see below
    – texts/ – example texts and tests to process by GNAT
    and two property files (isgn_ and service_properties.xml).

    2) Create and fill a relational database
    —————————————-

    Load the SQL data into a relational database, such as MySQL.
    a) set up a local database if none is already running
    b) create a schema for GNAT data, for instance, called “GNAT”
    c) load all the SQL files into that schema
    Example in MySQL:
    – change to the sql/ directory
    – start the MySQL command prompt
    – run the command
    > source gr_goterm.sql;
    to load the GOTerm table.
    For MySQL, you can also run
    > source mysqlImportAll.sql;
    to import all SQL files at once.
    Again, you need to be in the sql/ directory!
    d) download a JDBC driver for that database from the respective website, such
    as mysql-connector-java-5.1.7-bin.jar, and move it to the lib/ directory
    e) change the database-related settings in isgn_properties.xml, see below

    3) Edit property files
    ———————-

    Edit the properties file “isgn_properties.xml”
    – change the database access information according to your local configuration:
    change entries for dbUser, dbPass, dbAccessUrl, and dbDriver
    Examples:
    dbAccessUrl: jdbc:mysql://localhost:3306/GNAT
    dbDriver: org.gjt.mm.mysql.Driver

    4) Run a test
    ————-

    a) Start the human gene recognition dictionary locally
    – see scripts/startHuman.sh for an example of how to start a dictionary
    – you need to provide the path to the human (9606) dictionary directory,
    – as well as a port on which the server will run
    – needs around 2GB of memory
    – needs around 1min to start
    – after successful startup, the dictionary will display a message like
    “Startup in x seconds.”
    b) If you chose a port other than given in the example shell script (“56001”),
    edit data/taxonToServerPort.txt and change the entry for “9606” accordingly
    c) You can also run the dictionary on a different machine; in this case, add
    the machine’s address and port to the entry for 9606, in the format
    “server:port”. Example:
    9606[TAB]myserver.com:5555[TAB]optional comment
    d) Run the test with scripts/testLocalPipeline.sh
    e) The test should return the message “Test okay!” upon completion
    =========================================================
    Mình đã thực hiện đến bước 3 trong cả 2 môi trường Win và Ubuntu, nhưng đến bước 4 thì k làm được….hjc…rất mong thầy xem giúp ạ!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.