2018년 4월 10일 화요일

[Java] Tar Compress,Decompress Example



Downloadcommons-compress-1.16.1


1. Tar Compress Example

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static void tar( File baseDir, List<File> list, File outFile ) throws IOException {
    TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(outFile));
    taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
    taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    taos.setAddPaxHeadersForNonAsciiNames(true);
    if (list.size() > 0) {
        for (int i = 0; i < list.size(); i++) {
            String entryName = list.get(i).getPath().substring(baseDir.getPath().length()+1);
            taos.putArchiveEntry(new TarArchiveEntry(list.get(i), entryName));
            try (FileInputStream in = new FileInputStream(list.get(i))){
                byte[] buf = new byte[8 * 1024];
                int length;        
                while ((length = in.read(buf, 0, buf.length)) >= 0) {
                    taos.write(buf, 0, length);
                }  
                in.close();
                taos.closeArchiveEntry();
            }
        }        
    }
    taos.close();
}

static void tarTest() throws IOException {
    Compress cu = new Compress();
    List<File> filelist = new ArrayList<File>();
    filelist.add(new File("d:\\temp/qsort.c"));
    filelist.add(new File("d:\\temp\\a\\leaf.cpp"));
    File dir = new File("d:\\temp");
    File zippedFile = new File ( "d:\\temp\\test.tar");
    cu.tar(dir, filelist, zippedFile);  
}


2. Tar + gzip Compress Example

Put GzipCompressorOutputStream() when open TarArchiveOutputStream() 


1
TarArchiveOutputStream taos = new TarArchiveOutputStream(new GzipCompressorOutputStream(new FileOutputStream(name)));



3. Tar Decompress Example

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void untar( File inFile, File destDir ) throws IOException {
    TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(inFile));
    TarArchiveEntry entry;
    destDir.mkdirs();
    while ( (entry = tais.getNextTarEntry()) != null ){
        String name = entry.getName();
        File target = new File (destDir, name);
        System.out.println ("Entry : " + name);
        if ( entry.isDirectory() ){
            target.mkdirs();
        } else {
            if( ! target.getParentFile().exists() ) {
                target.getParentFile().mkdirs();
            }
            target.createNewFile();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
            byte [] buf = new byte[1024 * 8];
            int len = 0;
            while ((len = tais.read(buf)) >= 0 ){
                bos.write(buf, 0, len);
            }
            bos.close();
            System.out.println ("File : " + name);
        }
    }
    tais.close();        
}

static void untarTest() throws IOException {
    Compress cu = new Compress();
    File dir = new File("d:\\temp_tar");
    File zippedFile = new File ( "d:\\temp\\test.tar");        
    cu.untar( zippedFile, dir);
}


4. Tar + gzip Decompress Example

Put GzipCompressorInputStream() when open TarArchiveInputStream() 


1
TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(in)))



댓글 없음:

댓글 쓰기