javase解归档文件

 1 package com.archiver;
 2
 3 import java.util.List;
 4
 5 import org.junit.Test;
 6
 7 public class ArchiverDemo {
 8     /**
 9      * 测归档
10      */
11     @Test
12     public void testArchive() {
13         String archFile = "D:\\arch\\my.xar";
14         String txt = "D:\\a.txt";
15         String jpg = "E:\\迅雷下载\\b.jpg";
16         String ma3 = "E:\\迅雷下载\\c.mp3";
17         Archiver ar = new Archiver();
18         ar.archive(archFile, txt, jpg, ma3);
19     }
20
21     /**
22      * 测解档
23      */
24     @Test
25     public void testunArhive() {
26         String archFile = "D:\\arch\\my.xar";
27         Archiver ar = new Archiver();
28         ar.unArchiver(archFile, "D:\\arch\\unarch");
29     }
30     /**
31      * 测追加文件
32      */
33     @Test
34     public void testAppendFileToArhive() {
35         String archFile = "D:\\arch\\my.xar";
36         Archiver ar = new Archiver();
37         ar.appendFile(archFile, "D:\\arch\\d.jpg");
38     }
39
40     /**
41      * 测文档所有内容
42      */
43     @Test
44     public void testArchFileNames() {
45         Archiver ar = new Archiver();
46         List<String> list = ar.getAllFileName("D:\\arch\\my.xar");
47         for (String string : list) {
48             System.out.println(string);
49         }
50     }
51 }

  1 package com.archiver;
  2
  3 import java.io.FileInputStream;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8
  9 public class Archiver {
 10
 11     /**
 12      * 把多个文件归档到一个文件中
 13      */
 14     public void archive(String archFile, String... srcFiles) {
 15         for (String srcFile : srcFiles) {
 16             appendFile(archFile, srcFile);
 17         }
 18     }
 19
 20     /**
 21      * 向归档文件中追加内容
 22      *
 23      * @param archFile
 24      * @param srcFile
 25      */
 26     public void appendFile(String archFile, String srcFile) {
 27         FileOutputStream fos=null;
 28         FileInputStream fis=null;
 29         try {
 30              fos = new FileOutputStream(archFile, true);
 31             // 1.处理文件名
 32             String fileName = getName(srcFile);
 33             // 1.1 存储文件名长度
 34             byte[] byt = Int2Byte(fileName.length());
 35             fos.write(byt);
 36             // 1.2 文件字节数组
 37             fos.write(fileName.getBytes());
 38             // 2、处理文件内容
 39             fis = new FileInputStream(srcFile);
 40             int count=fis.available();//文件内容长度
 41             //存储文件内容长度
 42             fos.write(Int2Byte(count));
 43             byte[] buff = new byte[1024];
 44             int len = 0;
 45             while ((len = fis.read(buff)) != -1) {
 46                 fos.write(buff, 0, len);
 47             }
 48         } catch (Exception e) {
 49             // TODO Auto-generated catch block
 50             e.printStackTrace();
 51         } finally {
 52             try {
 53                 fis.close();
 54                 fos.close();
 55             } catch (IOException e) {
 56                 // TODO Auto-generated catch block
 57                 e.printStackTrace();
 58             }
 59         }
 60
 61     }
 62 /**
 63  * 解归档
 64  * @param length
 65  * @return
 66  */
 67     public void unArchiver(String archFile,String destDir){
 68         //FileInputStream fis=null;
 69         //FileOutputStream fos=null;
 70         try {
 71             FileInputStream fis=new FileInputStream(archFile);
 72             //读取文件名长度
 73             byte[] fnameLenBuff=new byte[4];
 74             while((fis.read(fnameLenBuff))!=-1){
 75
 76                 int fnameLength=Byte2Int(fnameLenBuff);
 77                 //读取文件名
 78                 byte[] fnameBuf=new byte[fnameLength];
 79                 fis.read(fnameBuf);
 80                 String fname=new String(fnameBuf);
 81                 //读取文件内容长度
 82                 byte[] fcontBuf=new byte[4];
 83                 fis.read(fcontBuf);
 84                 int flen=Byte2Int(fcontBuf);
 85                 //将归档内容写到指定目录处
 86                 FileOutputStream fos=new FileOutputStream(destDir+"\\"+fname);
 87                 byte[] buf=new byte[1024];
 88                 int mod=flen % buf.length;
 89                 int count=0;
 90
 91                 if(mod==0){
 92                     count=flen/buf.length;
 93                 }
 94                 else{
 95                     count=flen/buf.length+1;
 96                 }
 97                 for(int i=0;i<count;i++){
 98                     //最后一次
 99                     if(i==(count-1)){
100                         mod=(mod==0?buf.length:mod);
101                         fis.read(buf, 0, mod);
102                         fos.write(buf, 0, mod);
103                         fos.close();
104                     }
105                     else{
106                         fis.read(buf);
107                         fos.write(buf);
108                     }
109                 }
110             }
111             fis.close();
112         } catch (Exception e) {
113             e.printStackTrace();
114             // TODO: handle exception
115         }
116     }
117     /**
118      * 提取归档文件的所有文件名
119      * @param length
120      * @return
121      */
122         public List<String>  getAllFileName(String archFile){
123             //FileInputStream fis=null;
124             //FileOutputStream fos=null;
125             ArrayList<String> names = new ArrayList<>();
126             try {
127                 FileInputStream fis=new FileInputStream(archFile);
128                 //读取文件名长度
129                 byte[] fnameLenBuff=new byte[4];
130                 while((fis.read(fnameLenBuff))!=-1){
131
132                     int fnameLength=Byte2Int(fnameLenBuff);
133                     //读取文件名
134                     byte[] fnameBuf=new byte[fnameLength];
135                     fis.read(fnameBuf);
136                     String fname=new String(fnameBuf);
137                     names.add(fname);
138                     //读取文件内容长度
139                     byte[] fcontBuf=new byte[4];
140                     fis.read(fcontBuf);
141                     int flen=Byte2Int(fcontBuf);
142                     //跳过文件内容
143                     fis.skip(flen);
144                 }
145                 fis.close();
146             } catch (Exception e) {
147                 e.printStackTrace();
148                 // TODO: handle exception
149             }
150             return names;
151         }
152 //整数转为字节数组
153     private byte[] Int2Byte(int length) {
154         byte[] a=new byte[4];
155         a[0]=(byte) (length>>24);
156         a[1]=(byte) (length>>16);
157         a[2]=(byte) (length>>8);
158         a[3]=(byte) (length>>0);
159         return a;
160     }
161     //字节数组转为整数
162     private int Byte2Int(byte[] by) {
163         int a1=by[0]<<24;
164         int a2=(by[1]&0xff)<<16;
165         int a3=(by[2]&0xff)<<8;
166         int a4=(by[3]&0xff)<<0;
167
168         return a1|a2|a3|a4;
169     }
170     /**
171      * 获取文件名称
172      *
173      * @param srcFile
174      * @return
175      */
176 private String getName(String srcFilePath) {
177     String seg = null;
178     if(srcFilePath.contains("\\")){
179         seg="\\";
180     }
181     else if(srcFilePath.contains("/")){
182         seg="/";
183     }
184     return srcFilePath.substring(srcFilePath.lastIndexOf(seg)+1);
185     }
186 }
package com.archiver;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Archiver {

    /**
     * 把多个文件归档到一个文件中
     */
    public void archive(String archFile, String... srcFiles) {
        for (String srcFile : srcFiles) {
            appendFile(archFile, srcFile);
        }
    }

    /**
     * 向归档文件中追加内容
     *
     * @param archFile
     * @param srcFile
     */
    public void appendFile(String archFile, String srcFile) {
        FileOutputStream fos=null;
        FileInputStream fis=null;
        try {
             fos = new FileOutputStream(archFile, true);
            // 1.处理文件名
            String fileName = getName(srcFile);
            // 1.1 存储文件名长度
            byte[] byt = Int2Byte(fileName.length());
            fos.write(byt);
            // 1.2 文件字节数组
            fos.write(fileName.getBytes());
            // 2、处理文件内容
            fis = new FileInputStream(srcFile);
            int count=fis.available();//文件内容长度
            //存储文件内容长度
            fos.write(Int2Byte(count));
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fis.read(buff)) != -1) {
                fos.write(buff, 0, len);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
/**
 * 解归档
 * @param length
 * @return
 */
    public void unArchiver(String archFile,String destDir){
        //FileInputStream fis=null;
        //FileOutputStream fos=null;
        try {
            FileInputStream fis=new FileInputStream(archFile);
            //读取文件名长度
            byte[] fnameLenBuff=new byte[4];
            while((fis.read(fnameLenBuff))!=-1){

                int fnameLength=Byte2Int(fnameLenBuff);
                //读取文件名
                byte[] fnameBuf=new byte[fnameLength];
                fis.read(fnameBuf);
                String fname=new String(fnameBuf);
                //读取文件内容长度
                byte[] fcontBuf=new byte[4];
                fis.read(fcontBuf);
                int flen=Byte2Int(fcontBuf);
                //将归档内容写到指定目录处
                FileOutputStream fos=new FileOutputStream(destDir+"\\"+fname);
                byte[] buf=new byte[1024];
                int mod=flen % buf.length;
                int count=0;

                if(mod==0){
                    count=flen/buf.length;
                }
                else{
                    count=flen/buf.length+1;
                }
                for(int i=0;i<count;i++){
                    //最后一次
                    if(i==(count-1)){
                        mod=(mod==0?buf.length:mod);
                        fis.read(buf, 0, mod);
                        fos.write(buf, 0, mod);
                        fos.close();
                    }
                    else{
                        fis.read(buf);
                        fos.write(buf);
                    }
                }
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
    }
    /**
     * 提取归档文件的所有文件名
     * @param length
     * @return
     */
        public List<String>  getAllFileName(String archFile){
            //FileInputStream fis=null;
            //FileOutputStream fos=null;
            ArrayList<String> names = new ArrayList<>();
            try {
                FileInputStream fis=new FileInputStream(archFile);
                //读取文件名长度
                byte[] fnameLenBuff=new byte[4];
                while((fis.read(fnameLenBuff))!=-1){

                    int fnameLength=Byte2Int(fnameLenBuff);
                    //读取文件名
                    byte[] fnameBuf=new byte[fnameLength];
                    fis.read(fnameBuf);
                    String fname=new String(fnameBuf);
                    names.add(fname);
                    //读取文件内容长度
                    byte[] fcontBuf=new byte[4];
                    fis.read(fcontBuf);
                    int flen=Byte2Int(fcontBuf);
                    //跳过文件内容
                    fis.skip(flen);
                }
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
                // TODO: handle exception
            }
            return names;
        }
//整数转为字节数组
    private byte[] Int2Byte(int length) {
        byte[] a=new byte[4];
        a[0]=(byte) (length>>24);
        a[1]=(byte) (length>>16);
        a[2]=(byte) (length>>8);
        a[3]=(byte) (length>>0);
        return a;
    }
    //字节数组转为整数
    private int Byte2Int(byte[] by) {
        int a1=by[0]<<24;
        int a2=(by[1]&0xff)<<16;
        int a3=(by[2]&0xff)<<8;
        int a4=(by[3]&0xff)<<0;

        return a1|a2|a3|a4;
    }
    /**
     * 获取文件名称
     *
     * @param srcFile
     * @return
     */
private String getName(String srcFilePath) {
    String seg = null;
    if(srcFilePath.contains("\\")){
        seg="\\";
    }
    else if(srcFilePath.contains("/")){
        seg="/";
    }
    return srcFilePath.substring(srcFilePath.lastIndexOf(seg)+1);
    }
}

时间: 2024-08-01 04:57:51

javase解归档文件的相关文章

大数据技术之_04_Hadoop学习_02_HDFS_DataNode(面试开发重点)+HDFS 2.X新特性

第6章 DataNode(面试开发重点)6.1 DataNode工作机制6.2 数据完整性6.3 掉线时限参数设置6.4 服役新数据节点6.5 退役旧数据节点6.5.1 添加白名单6.5.2 黑名单退役6.6 Datanode多目录配置第7章 HDFS 2.X新特性7.1 集群间数据拷贝7.2 小文件存档7.3 回收站7.4 快照管理 第6章 DataNode(面试开发重点) 6.1 DataNode工作机制 DataNode工作机制,如下图所示. 1)一个数据块在DataNode上以文件形式存

大数据技术之Hadoop(HDFS)

第1章 HDFS概述 1.1 HDFS产出背景及定义 1.2 HDFS优缺点 1.3 HDFS组成架构 1.4 HDFS文件块大小(面试重点) 第2章 HDFS的Shell操作(开发重点) 1.基本语法 bin/hadoop fs 具体命令 OR bin/hdfs dfs 具体命令 dfs是fs的实现类. 2.命令大全 [[email protected] hadoop-2.7.2]$ bin/hadoop fs ? [-appendToFile <localsrc> ... <dst&

hadoop小文件存档

hadoop小文件存档1.HDFS存档小文件弊端 每个文件均按块存储,每个块的元数据存储在NameNode的内存中,因此HDFS存储小文件会非常低效.因为大量的小文件会耗尽NameNode中的大部分内存.但注意,存储小文件所需的磁盘容量和数据块的大小无关.例如,一个1M的文件设置为128M的块存储,实际使用的是1M的磁盘你空间.2.解决存储小文件办法之一 HDFS存文档文件或HAR文件,是一个更高效的文件存档工具,它将文件存入HDFS块,在减少NameNode内存使用的同时,允许对文件进行透明的

基于JavaSE阶段的IO流详解

1.IO流基本概述 在Java语言中定义了许多针对不同的传输方式,最基本的就是输入输出流(俗称IO流),IO流是属于java.io包下的内容,在JavaSE阶段主要学下图所示的: 其中从图中可知,所有输入流类都是抽象类,是InputStream或者抽象类Reader的子类:而所有输出流都是抽象类,是OutputStream或者Writer的子类.输入输出流的定义是根据流向所决定的,我们可以是本地为一个实体物质,从外界往本地输入,按照本地的状态就是读取,反之,从本地向外写入就是输出.IO流是最基本

JavaSE数组详解与实战案例应用

1.数组声明: 第一种:数据类型 变量名[],例如:int x[]; 第二种:数据类型[] 变量名=new 数据类型[要在内存中开辟的空间数量,例如:int[] x=new int[3]; 第三种:数据类型[] 变量名=new 数据类型[]{元素1,元素2,元素3}; 例子1: public staticvoidmain(String[] arg){ //定义一个字符串数组,将各个元素(也就是月饼)放入其中 String[]names={"五仁月饼","香辣牛肉月饼"

JavaSE之Long 详解 Long的方法简介以及用法

基本功能 Long 类在对象中包装了基本类型 long 的值 每个 Long 类型的对象都包含一个 long 类型的字段 static long MAX_VALUE long 8个字节最大值2^63-1十六进制:  0x8000000000000000L static long MIN_VALUE 最小值-2^63十六进制:0x7fffffffffffffffL static int SIZE 二进制补码形式表示值时的位数 @Native public static final int SIZE

Tomcat配置详解

一.Tomcat定义 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选.对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应对HTML页面的访问请求.实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的 java基础 Java体系结构包括四

Java8初体验(二)Stream语法详解

原文链接:http://ifeve.com/stream/ 1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel aggregate operations. 我们来解读一下上面的那句话: Stream是元素的集合,这点让Stream看起来用些类似Iterator: 可以支持顺序和并行的对原Stream进行汇聚的操作: 大家可以把Stream当成一个高级版本的

Linux下的tar压缩解压缩命令详解

tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的参数是根据需要在压缩或解压档案时可选的.-z:有gzip属性的 -j:有bz2属性的 -Z:有compress属性的 -v:显示所有过程 -O:将文件解开到标准输出 下面的参数-f是必须的 -f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名. # tar -cf all.t