Как извлечь файлы из файла архива:

java.util.zip предоставляет классы для извлечения, читать и писать Zip файлов.

Example below extracts files from given zip file into destination folder.


import java.io.*;

import java.util.zip.*;



class  testZipFiles 

{

    public static void main(String[] args

    {

         if (args.length != 1

        {

            System.out.println("Usage: java testFiles [zipfile path] ");

            return;

        }

        try

        {

            String filename = args[0];

            testZipFiles list = new testZipFiles( );

            list.getZipFiles(filename);

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }



    public void getZipFiles(String filename)

    {

        try

        {

            String destinationname = "d:\\servlet\\testZip\\";

            byte[] buf = new byte[1024];

            ZipInputStream zipinputstream = null;

            ZipEntry zipentry;

            zipinputstream = new ZipInputStream(

                new FileInputStream(filename));



            zipentry = zipinputstream.getNextEntry();

            while (zipentry != null

            

                //for each entry to be extracted

                String entryName = zipentry.getName();

                System.out.println("entryname "+entryName);

                int n;

                FileOutputStream fileoutputstream;

                File newFile = new File(entryName);

                String directory = newFile.getParent();

                

                if(directory == null)

                {

                    if(newFile.isDirectory())

                        break;

                }

                

                fileoutputstream = new FileOutputStream(

                   destinationname+entryName);             



                while ((n = zipinputstream.read(buf, 01024)) > -1)

                    fileoutputstream.write(buf, 0, n);



                fileoutputstream.close()

                zipinputstream.closeEntry();

                zipentry = zipinputstream.getNextEntry();



            }//while



            zipinputstream.close();

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }

}

Example below extracts files from given zip file into destination folder.


import java.io.*;

import java.util.zip.*;



class  testZipFiles 

{

    public static void main(String[] args

    {

         if (args.length != 1

        {

            System.out.println("Usage: java testFiles [zipfile path] ");

            return;

        }

        try

        {

            String filename = args[0];

            testZipFiles list = new testZipFiles( );

            list.getZipFiles(filename);

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }



    public void getZipFiles(String filename)

    {

        try

        {

            String destinationname = "d:\\servlet\\testZip\\";

            byte[] buf = new byte[1024];

            ZipInputStream zipinputstream = null;

            ZipEntry zipentry;

            zipinputstream = new ZipInputStream(

                new FileInputStream(filename));



            zipentry = zipinputstream.getNextEntry();

            while (zipentry != null

            

                //for each entry to be extracted

                String entryName = zipentry.getName();

                System.out.println("entryname "+entryName);

                int n;

                FileOutputStream fileoutputstream;

                File newFile = new File(entryName);

                String directory = newFile.getParent();

                

                if(directory == null)

                {

                    if(newFile.isDirectory())

                        break;

                }

                

                fileoutputstream = new FileOutputStream(

                   destinationname+entryName);             



                while ((n = zipinputstream.read(buf, 01024)) > -1)

                    fileoutputstream.write(buf, 0, n);



                fileoutputstream.close()

                zipinputstream.closeEntry();

                zipentry = zipinputstream.getNextEntry();



            }//while



            zipinputstream.close();

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }

}

import java.io.*;

import java.util.zip.*;



class  testZipFiles 

{

    public static void main(String[] args

    {

         if (args.length != 1

        {

            System.out.println("Usage: java testFiles [zipfile path] ");

            return;

        }

        try

        {

            String filename = args[0];

            testZipFiles list = new testZipFiles( );

            list.getZipFiles(filename);

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }



    public void getZipFiles(String filename)

    {

        try

        {

            String destinationname = "d:\\servlet\\testZip\\";

            byte[] buf = new byte[1024];

            ZipInputStream zipinputstream = null;

            ZipEntry zipentry;

            zipinputstream = new ZipInputStream(

                new FileInputStream(filename));



            zipentry = zipinputstream.getNextEntry();

            while (zipentry != null

            

                //for each entry to be extracted

                String entryName = zipentry.getName();

                System.out.println("entryname "+entryName);

                int n;

                FileOutputStream fileoutputstream;

                File newFile = new File(entryName);

                String directory = newFile.getParent();

                

                if(directory == null)

                {

                    if(newFile.isDirectory())

                        break;

                }

                

                fileoutputstream = new FileOutputStream(

                   destinationname+entryName);             



                while ((n = zipinputstream.read(buf, 01024)) > -1)

                    fileoutputstream.write(buf, 0, n);



                fileoutputstream.close()

                zipinputstream.closeEntry();

                zipentry = zipinputstream.getNextEntry();



            }//while



            zipinputstream.close();

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

    }

}

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Можно использовать следующие HTML-теги и атрибуты: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>