笔记101 | 文件的压缩与解压笔记

时间:2022-07-22
本文章向大家介绍笔记101 | 文件的压缩与解压笔记,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.解压

1.1读取压缩文件

bean

public class Logo {
       public String mName;
       public String mPath;
       public Bitmap mBitmap32;
       public int pi;
}

主要逻辑方法

private void loadZip() {

   // get a zip file instance
   File file = new File("/system/etc/freq_logo.zip");
   if(file.canRead() && file.isFile()) {
       try {
           // create a ZipInputStream instance
           ZipInputStream is = null;
           try {
               is = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
               // create a ZipEntry instance , lay the every file from
               // decompress file temporarily
               ZipEntry entry = null;
               // a circle to get every file
               while((entry = is.getNextEntry()) != null) {
                   addFile("/system/etc/freq_logo.zip", entry.getName());
               }
           } catch (Exception e) {
           } finally {
               if(is != null) {
                   is.close();
               }
           }
       } catch (Exception e) {
       }
   }
}

private void addFile(String path, String name) {
   // create a AsyncTask addFile
   new AddFileTask().execute(path, name);
}
private class AddFileTask extends AsyncTask<String, Integer, Logo> {

   @Override
   protected Logo doInBackground(String... params) {

       try {
           String path = params[0];
           String name = params[1];
           Logo l = new Logo();
           if(name != null && name.length() > 0) {
               l.mName = name;
               l.pi = Integer.parseInt(name.substring(15, 19));
           }
           l.mPath = path;
           return l;
       } catch (Exception e) {
       }
       return null;
   }
   @Override
   protected void onPostExecute(Logo result) {
       if(result == null) return;
       Log.i("md",result.mName+ " "+result.mPath+" "+result.pi+"  "+result.mBitmap32);
       mLogo.add(result); //add result data
   }
}

由于考虑到文件数量,所以使用的是AddFileTask异步读取 此逻辑并没有把zip文件解压出来,而是以读文件的形式去获取内容,保存到mLogo 打印如下

2020-07-10 16:46:38.325 2274-2274/com.tw.radio I/md: freq_logo/logo_1024.jpg /system/etc/freq_logo.zip 1024  null
2020-07-10 16:46:38.325 2274-2274/com.tw.radio I/md: freq_logo/logo_1028.jpg /system/etc/freq_logo.zip 1028  null
2020-07-10 16:46:38.326 2274-2274/com.tw.radio I/md: freq_logo/logo_1054.jpg /system/etc/freq_logo.zip 1054  null

1.2解压文件

如果要实现解压成文件,可以参考以下逻辑

public void TestZipInputStream() throws IOException {
   // get a zip file instance
   File file = new File ( "/system/etc/freq_logo.zip" ) ;
   // get a ZipFile instance
   ZipFile zipFile = new ZipFile ( file ) ;
   // create a ZipInputStream instance
   ZipInputStream zis = new ZipInputStream ( new FileInputStream (
           file ) ) ;
   // create a ZipEntry instance , lay the every file from
   // decompress file temporarily
   ZipEntry entry = null ;
   // a circle to get every file
   while ( ( entry = zis.getNextEntry ( ) ) != null )
   {
       // define the path to set the file
       File outFile = new File ( "/data/tw/freq_logo/"
               + entry.getName ( ) ) ;
       // if the file's parent directory wasn't exits ,than
       // create the directory
       if ( !outFile.getParentFile ().exists ()){
           outFile.getParentFile ().mkdir () ;
       }
       // if the file not exits ,than create the file
       if (!outFile.exists ()){
           outFile.createNewFile ();
       }
       // create an input stream
       BufferedInputStream bis = new BufferedInputStream (zipFile.getInputStream (entry)) ;
       // create an output stream
       BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(outFile)) ;
       byte [] b = new byte [100] ;
       while (true) {
           int len = bis.read (b) ;
           if (len == - 1)
               break ;
           bos.write (b , 0 , len) ;
       }
       // close stream
       bis.close ( ) ;
       bos.close ( ) ;

   }
   zis.close ( ) ;
}

获取的文件如下

image.png

2.压缩

2.1文件的压缩:

public class TestFile
{
    public static void main ( String [ ] args ) throws IOException
    {
        // new a file input stream
        FileInputStream fis = new FileInputStream (
                "/home/liangruihua/ziptest/1.txt" ) ;
        BufferedInputStream bis = new BufferedInputStream ( fis ) ;

        // new a zipPutputStream
        // /home/liangruihua/ziptest/1.zip -- the out put file path and
        // name
        ZipOutputStream zos = new ZipOutputStream (
                new FileOutputStream (
                        "/home/liangruihua/ziptest/1.zip" ) ) ;
        BufferedOutputStream bos = new BufferedOutputStream ( zos ) ;

        // set the file name in the .zip file
        zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;

        // set the declear
        zos.setComment ( "by liangruihua test!" ) ;
    byte [ ] b = new byte [1024] ;
       int ch = 0;
       while ( true )
       {
           int len = bis.read ( b ) ;
           if ( len == - 1 )
               break ;
           bos.write ( b , 0 , len ) ;
       }
       bos.flush();
}

2.2文件夹的压缩

public class TestDir
{
    public static void main ( String [ ] args ) throws IOException
    {
        // the file path need to compress
        File file = new File ( "/home/liangruihua/ziptest/test" ) ;
        ZipOutputStream zos = new ZipOutputStream (
                new FileOutputStream (
                        "/home/liangruihua/ziptest/test.zip" ) ) ;

        // judge the file is the directory
        if ( file.isDirectory ( ) )
        {
            // get the every file in the directory
            File [ ] files = file.listFiles ( ) ;

            for ( int i = 0 ; i < files.length ; i ++ )
            {
                // new the BuuferedInputStream
                BufferedInputStream bis = new BufferedInputStream (
                        new FileInputStream (
                                files [ i ] ) ) ;
                // the file entry ,set the file name in the zip
                // file
                zos.putNextEntry ( new ZipEntry ( file
                        .getName ( )
                        + file.separator
                        + files [ i ].getName ( ) ) ) ;
                while ( true )
                {
                    byte [ ] b = new byte [ 100 ] ;
                    int len = bis.read ( b ) ;
                    if ( len == - 1 )
                        break ;
                    zos.write ( b , 0 , len ) ;
                }

                // close the input stream
                bis.close ( ) ;
            }

        }
        // close the zip output stream
        zos.close ( ) ;
    }
}