JAVA

java directory copy / progress bar

girin_dev 2022. 6. 11. 22:13
728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ˜’ test์šฉ์œผ๋กœ ๊ตฌํ˜„ java 1.8 ์‚ฌ์šฉ 

 

๐Ÿ˜’ ์—์ด์ „ํŠธ์˜ FS ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ ์ค‘ ํ•„์š”ํ•ด์„œ ๊ตฌํ˜„.

 

 

ํŠน์ • ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ ๋ฐ ๋ชจ๋“  ํ•˜์œ„ ํŒŒ์ผ์„ file output stream์œผ๋กœ ์“ฐ๋ฉด์„œ ์†Œ์Šค์™€ ํƒ€๊ฒŸ์˜ ๋ฐ”์ดํŠธ๋‹จ์œ„ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•œ๋‹ค.

์žฌ๊ท€ํ˜ธ์ถœ์„ ํ†ตํ•ด ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋„ ๋ชจ๋‘ ํ™•์ธํ•˜๋„๋ก ๊ตฌํ˜„.

 

1. static ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ง„ํ–‰๋ฅ  ๋“ฑ ๊ธฐ๋กํ•˜๋Š” ๋ฐฉ์‹. 

2. static ๋ณ€์ˆ˜ ์—†์ด, ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹.

     ๋ฉ€ํ‹ฐ  ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ์ „์—ญ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๊ผฌ์ด๋ฏ€๋กœ, 2๋ฒˆ์˜ ๋ฐฉ์‹์ด ์ ํ•ฉํ•จ.

 

 

FileDownloaderTest.java

package test;

import com.gemiso.ariel.common.util.FileUtil;

import java.io.*;

public class FsCopyDirTest {

    static long totalBytesCopied;
    static long expectedBytes;

    public static void main(String[] args) {
        String srcDirPath = "C:\\Users\\user\\Desktop\\ํ…Œ์ŠคํŠธ์šฉ ์˜์ƒ ๋ฐ ์ด๋ฏธ์ง€\\๋””๋ ‰ํ† ๋ฆฌ ์นดํ”ผ ํ…Œ์ŠคํŠธ์šฉ 1";
        //String srcDirPath = "C:\\Users\\admin\\Desktop\\๋‹ค์šด๋กœ๋“œ ์†Œ์Šค";
        String tgtDirPath = "C:\\Users\\user\\Desktop\\ํ…Œ์ŠคํŠธ์šฉ ์˜์ƒ ๋ฐ ์ด๋ฏธ์ง€\\๋””๋ ‰ํ† ๋ฆฌ ์นดํ”ผ ํƒ€๊ฒŸ";
        //String tgtDirPath = "C:\\Users\\admin\\Desktop\\๋‹ค์šด๋กœ๋“œ ํƒ€๊ฒŸ";

        File srcDir = new File(srcDirPath);
        File tgtDir = new File(tgtDirPath);

        expectedBytes = FileUtil.getDirSize(srcDir);
        
        // ์ „์—ญ๋ณ€์ˆ˜ ์—†์ด ์‹คํ–‰ํ•˜๊ฒŒ๋œ๋‹ค๋ฉด 0๊ฐ’์œผ๋กœ ์‹คํ–‰.
        // expectedBytes = Fileutil.getDirSize(srcDir,0);
        
        copy(srcDir, tgtDir);
    }


    public static void copy(File srcDir, File tgtDir) {

        boolean isDirectory = srcDir.isDirectory();

        String[] srcList = srcDir.list();

        boolean is_cancel = false;

        try {
            int len = 0;
            byte[] buf = new byte[1024];
            int rates = 0;

            System.out.println(" ์นดํ”ผ ์‹œ์ž‘! ");

            File finalTgtDir = tgtDir;
            File[] target_file = srcDir.listFiles();

            // ํด๋” ์‚ฌ์ด์ฆˆ๋ฅผ ๊ตฌํ•  ๊ฒฝ์šฐ ํ•˜์œ„ํŒŒ์ผ์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๊ณ  ํŒŒ์ผ ํฌ๊ธฐ ํ•ฉ์„ ๊ตฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
            // directory์˜ ์‚ฌ์ด์ฆˆ๋ฅผ 4096์œผ๋กœ ๊ณ ์ •ํ•ด์„œ ๋ฑ‰์–ด๋‚ธ๋‹ค.

            System.out.println(" ์†Œ์Šค ํŒŒ์ผ ์‚ฌ์ด์ฆˆ : " + expectedBytes);

            for (File file : target_file) {
                File temp = new File(tgtDir.getAbsolutePath() + File.separator + file.getName());
                if (file.isDirectory()) {
                    temp.mkdir();
                    copy(file, temp);
                } else {

                    FileInputStream fis = null;
                    FileOutputStream fos = null;
                    try {
                        fis = new FileInputStream(file);
                        fos = new FileOutputStream(temp);
                        byte[] b = new byte[1024];
                        while ((len = fis.read(b)) != -1) {
                            totalBytesCopied += len;

                            fos.write(b, 0, len);

                            rates = (int) Math.floor(( totalBytesCopied * 1.0 / expectedBytes) * 1000) / 10;

                            System.out.println(" ํŒŒ์ผ ๋ณ„ ์‚ฌ์ด์ฆˆ : " + totalBytesCopied + " / ์›๋ณธ dir ์‚ฌ์ด์ฆˆ : " + expectedBytes);
                            System.out.println(" ์ง„ํ–‰๋ฅ  : " + rates);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fis.close();
                            fos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            }

        } catch (Exception e) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pinrtStream = new PrintStream(out);
            e.printStackTrace(pinrtStream);
            System.out.println(out.toString());
        }

    }
    
    
    // ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์Šคํƒœํ‹ฑ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ ์•„๋ž˜ ๋กœ์ง ์ˆ˜ํ–‰ : 
     public static void copy(File srcDir, File tgtDir, long totalBytesCopied, long expectedBytes) {

        boolean isDirectory = srcDir.isDirectory();

        String[] srcList = srcDir.list();

        boolean is_cancel = false;

        try {
            int len = 0;
            byte[] buf = new byte[1024];

            System.out.println(" ์นดํ”ผ ์‹œ์ž‘! ");

            File finalTgtDir = tgtDir;
            File[] target_file = srcDir.listFiles();

            for (File file : target_file) {
                File temp = new File(tgtDir.getAbsolutePath() + File.separator + file.getName());
                if (file.isDirectory()) {
                    temp.mkdir();
                    copy(file, temp, totalBytesCopied, expectedBytes);
                } else {

                    FileInputStream fis = null;
                    FileOutputStream fos = null;
                    DataOutputStream dos = null;
                    try {
                        fis = new FileInputStream(file);
                        fos = new FileOutputStream(temp);
                        dos = new DataOutputStream(fos);


                        byte[] b = new byte[1024];
                        while ((len = fis.read(b)) != -1) {
                            totalBytesCopied += len;
                            dos.write(b, 0, len);

                            FileUtil.rates = (int) Math.floor(( totalBytesCopied * 1.0 / expectedBytes) * 1000) / 10;

                            System.out.println(" ํŒŒ์ผ ๋ณ„ ์‚ฌ์ด์ฆˆ : " + totalBytesCopied + " / ์›๋ณธ dir ์‚ฌ์ด์ฆˆ : " + expectedBytes + " / rate : " + FileUtil.rates);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fis.close();
                            fos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            }

        } catch (Exception e) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pinrtStream = new PrintStream(out);
            e.printStackTrace(pinrtStream);
            System.out.println(out.toString());
        }

    }

    
    
    
    
    
}

 

 

FileUtil.java

package com.org.walk.util;

import java.io.File;
import java.io.IOException;

public class Utils {

 	public static long getDirSize(File folder) {
		File[] files = folder.listFiles();

		long length=0;

		int count = files.length;
		for (int i = 0; i < count; i++) {
			if (files[i].isFile()) {
				// System.out.println(" ํŒŒ์ผ ๋„ค์ž„ : " + files[i].getName() + " / size : " + files[i].length());
				length += files[i].length();
			} else {
				// System.out.println(" DIR ๋„ค์ž„ : " + files[i].getName());
				length += getDirSize(files[i]);
			}
		}
		return length;
	}
    
    
    // ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ static ๋ณ€์ˆ˜๋ฅผ ๋™์‹œ์— ์ฐธ์กฐํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ผ๋ฉด ์•„๋ž˜ ๋กœ์ง ์ˆ˜ํ–‰ : 
    public static long getDirSize(File folder, long length) {
		File[] files = folder.listFiles();

		long _length;

		if (length!=0) {
			_length = length;
		} else {
			_length = 0;
		}

		int count = files.length;
		for (int i = 0; i < count; i++) {
			if (files[i].isFile()) {
				
				_length += files[i].length();
				
			} else {
				
				_length += getDirSize(files[i],0);
				
			}
		}

		return _length;
	}
    
    
    
    
}

 

 

 

 

 

 

 

 

 

320x100
๋ฐ˜์‘ํ˜•