Discuss / Java / 打卡~~

打卡~~

Topic source

找了半天,发现getFileDataAsBytes()这个函数java标准库中并没有,自己写了一个~

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
 * @authur    zhangqizky
 * @apiNote  操作zip:ZipInputStream和JarInputStream
 */

 public class MyZip
 {
     public static void main(String[]args)throws Exception
     {
         /**
          * @从zip文件中读取
          */
        try(ZipInputStream zip = new ZipInputStream(new FileInputStream("day02.zip")))
        {
            ZipEntry entry = null;
            while((entry = zip.getNextEntry()) != null)
            {
                String name = entry.getName();
                if(!entry.isDirectory())
                {
                    int n;
                    while((n=zip.read())!=-1)
                    {
                        System.out.println(n);
                    }
                }
            }

        }
        /**
         * @写入Zip文件
         */
        try(ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("out.zip")))
        {
            File[]files = {new File("MyFile.java"),new File("MyFilter.java")};
            for (File f :files)
            {
                zip.putNextEntry(new ZipEntry(f.getName()));
                zip.write(getFileDataAsBytes(f));
                zip.closeEntry();
            }
        }
     }
     public static byte[] getFileDataAsBytes(File f)
     {
         byte[] data = new byte[1024*1024];
         try(InputStream is =new FileInputStream(f))
         {
             int n;
             while((n=is.read(data))!=-1)
             {
                 System.out.println("read"+n+"bytes");
             }

         }catch(IOException e)
         {
             e.printStackTrace();
         }
         return data;
     }
 }

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

你那个getFileDataAsBytes写错了,始终返回固定大小的byte[]

酱紫行不行啊廖老师?

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
 * @authur    zhangqizky
 * @apiNote  操作zip:ZipInputStream和JarInputStream
 */

 public class MyZip
 {
     public static void main(String[]args)throws Exception
     {
         /**
          * @从zip文件中读取
          */
        try(ZipInputStream zip = new ZipInputStream(new FileInputStream("day02.zip")))
        {
            ZipEntry entry = null;
            while((entry = zip.getNextEntry()) != null)
            {
                String name = entry.getName();
                if(!entry.isDirectory())
                {
                    int n;
                    while((n=zip.read())!=-1)
                    {
                        System.out.println(n);
                    }
                }
            }

        }
        /**
         * @写入Zip文件
         */
        try(ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("out.zip")))
        {
            File[]files = {new File("MyFile.java"),new File("MyFilter.java")};
            for (File f :files)
            {
                zip.putNextEntry(new ZipEntry(f.getName()));
                zip.write(getFileDataAsBytes(f));
                zip.closeEntry();
            }
        }
     }
     public static byte[] getFileDataAsBytes(File f)
     {
         byte[] data = new byte[(int)f.length()];
         try(InputStream is =new FileInputStream(f))
         {
             int n;
             while((n=is.read(data))!=-1)
             {
                 System.out.println("read"+n+"bytes");
             }

         }catch(IOException e)
         {
             e.printStackTrace();
         }
         return data;
     }
 }

廖雪峰

#4 Created at ... [Delete] [Delete and Lock User]

准确地说:

byte[] data = new byte[(int)f.length()];
try(InputStream is =new FileInputStream(f))

你这两行代码之间,有可能文件会被其他程序修改,导致实际读取的字节与文件长度不一致

用ByteArrayOutputStream,那个就是用来在内存中开辟缓存的。


  • 1

Reply