android文件复制、清空文件夹、压缩文件

    private static final int BUFFER = 1024 * 10;
    /**
    * copy file1 to folder file2
    * @param file1
    * @param file2
    */
   public static void copy(String file1, String file2) {
       File src = new File(file1);
       File dst = new File(file2);
       if (!dst.exists()) {
           dst.mkdirs();
       }
       InputStream in = null;
       OutputStream out = null;
       try {
           in = new BufferedInputStream(new FileInputStream(src), 16 * 1024);
           FileOutputStream f = new FileOutputStream(dst + file1.substring(file1.lastIndexOf("/"))); //一定要加上文件名称
           out = new BufferedOutputStream(f, 16 * 1024);
           byte[] buffer = new byte[16 * 1024];
           int len = 0;
           while ((len = in.read(buffer)) > 0) {
               out.write(buffer, 0, len);
           }
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (null != in) {
               try {
                   in.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if (null != out) {
               try {
                   out.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
   }

   /**
    * pack file folder to zip
    * @param sourceFilePaths source files path
    * @param zipFilePath dest zip path
    * @param fileName zip file name
    * @return
    */
   public static boolean fileToZip(String[] sourceFilePaths, String zipFilePath, String fileName){
       boolean flag = false;
       File zipFile = new File(zipFilePath + "/" + fileName +".zip");
       if(zipFile.exists()){
           Log.d(TAG, zipFilePath + " The file already exists in the directory " + fileName +".zip");
           delete(zipFilePath + "/" + fileName +".zip");
       }
       FileOutputStream fos = null;
       try {
           fos = new FileOutputStream(zipFile);
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
       File zipFolder = new File(zipFilePath);
       if (!zipFolder.exists()) {
           zipFolder.mkdirs();
       }
       for (String sourceFilePath : sourceFilePaths) {
           File sourceFile = new File(sourceFilePath);
           if(!sourceFile.exists()){
               Log.d(TAG, "The file to be compressed does not exist " + sourceFilePath);
           }else{
               flag = compress(sourceFile, zos);
               if (!flag) {
                   return false;
               }
           }
       }
       try {
           zos.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return flag;
   }

   /**
    * compress file and folder
    * @param file file to compress
    * @param out
    * @return
    */
   public static boolean compress(File file, ZipOutputStream out) {
       boolean flag = false;
       if (file.isFile()) {
           if (!file.exists()) {
               return false;
           }
           BufferedInputStream bis = null;
           try {
               bis = new BufferedInputStream(new FileInputStream(file));
               ZipEntry entry = new ZipEntry(file.getName());
               out.putNextEntry(entry);
               int count;
               byte data[] = new byte[BUFFER];
               while ((count = bis.read(data, 0, BUFFER)) != -1) {
                   out.write(data, 0, count);
               }
               flag = true;
           } catch (IOException e) {
               e.printStackTrace();
           } finally {
               try {
                   if (bis != null) bis.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       } else {
           File[] files = file.listFiles();
           if (files != null && files.length != 0){
               for (int i = 0; i < files.length; i++) {
                   if (!compress(files[i], out)) {
                       return false;
                   }
               }
           }
           flag = true;
       }
       return flag;
   }

   /**
    * clear file folder
    * @param path
    */
   public static void delete(String path) {
       File file = new File(path);
       File[] files = file.listFiles();
       if (files != null && files.length > 0) {
           for(File temp : files){
               if (temp.isFile()) {
                   temp.delete();
               }
               if (temp.isDirectory()) {
                   delete(temp.getAbsolutePath());
                   temp.delete();
               }
           }
       }
   }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容