流:简单的压缩和解压流程

package guidang;

/**
 * Created by Administrator on 2017/7/29.
 */
public class TestArchive {
    public static void main(String[] args) {
        Archiver a = new Archiver();
        a.appendFile("E:\\study\\徐老师\\day02\\big6-01java-day02\\123.xar", "E:\\study\\徐老师\\day02\\big6-01java-day02\\archive\\1.txt");
        a.appendFile("E:\\study\\徐老师\\day02\\big6-01java-day02\\123.xar", "E:\\study\\徐老师\\day02\\big6-01java-day02\\archive\\2.jpg");
        a.appendFile("E:\\study\\徐老师\\day02\\big6-01java-day02\\123.xar", "E:\\study\\徐老师\\day02\\big6-01java-day02\\archive\\3.mp3");

        a.unarchiveFile("E:\\study\\徐老师\\day02\\big6-01java-day02\\123.xar", "E:\\study\\徐老师\\day02\\big6-01java-day02\\123");


    }


}

压缩和解压的报文:
这里写图片描述

package guidang;


import java.io.*;

/**
 * Created by Administrator on 2017/7/29.
 */
public class Archiver {

    //将file文件追加到xar中
    public void appendFile(String xar, String file) {
        try {
            //归档文件输出流
            FileOutputStream fos = new FileOutputStream(xar, true);

            //1.文件名长度
            File f = new File(file);
            String fileName = f.getName();
            byte[] fileNameBytes = fileName.getBytes();
            byte[] fileNameLenBytes = DataUtil.int2ByteArrr(fileNameBytes.length);
            fos.write(fileNameLenBytes);
            //2.文件名内容
            fos.write(fileNameBytes);
            //3.文件长度
            int len = (int)f.length();
            byte[] lenBytes =  DataUtil.int2ByteArrr(len);
            fos.write(lenBytes);
            //4.文件内容
            FileInputStream fis = new FileInputStream(f);
            byte[] buf = new byte[1024];
            int len0 = 0;
            while((len0=fis.read(buf)) != -1){
                fos.write(buf,0,len0);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //讲file文件追加到xar中
    public void unarchiveFile(String xar, String dir) {
        try {
            FileInputStream fis = new FileInputStream(xar);
            while(true){
                byte[] byte4 = new byte[4];
                int len = fis.read(byte4);
                //len=-1说明这是最后一个需要解压的文件
                if (len == -1){
                    break;
                }
                byte[] fileNameBytes = new byte[DataUtil.byteArrr2Int(byte4)];
                //读取文件名
                fis.read(fileNameBytes);
                String fileName = new String(fileNameBytes);
                File newFile = new File(dir,fileName);
                FileOutputStream fos = new FileOutputStream(newFile);

                //读取文件长度
                fis.read(byte4);
                byte[] fileContBytes = new byte[DataUtil.byteArrr2Int(byte4)];
                fis.read(fileContBytes);
                fos.write(fileContBytes);
                fos.close();
            }
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

这里写图片描述

byte数组转换成整数思路:

1)文件名长度和文件内容长度都是4个字节,所以在取出他们的时候只需要4个字节的转换即可。
2)取出每一个字节的值后,如果直接位移的话就会先自动转换成int型再位移这样就会有正负符号的问题,解决它的办法就是先让它和0xff(11111111)进行&(与)运算,这样左边的24,16,8位就都能确定是0了。
3)每个字节值的符号问题解决后或(|)一下就可以算出值了。


1)byte short char 在进行计算(移位,加减乘除等)的时候都会先转成int类型再计算
2)>>>是有符号右移,前面全部补1;>>是无符号右移,前面全部补0
    package guidang;

/**
 * Created by Administrator on 2017/7/29.
 */
public class DataUtil {

    //byte short char 在进行计算(移位,加减乘除等)的时候都会先转成int类型再计算
    //>>>是有符号右移,前面全部补1;>>是无符号右移,前面全部补0

    //将正数转换成字节数组
    public  static byte[] int2ByteArrr(int i){
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (i >> 24);
        bytes[1] = (byte) (i >> 16);
        bytes[2] = (byte) (i >> 8);
        bytes[3] = (byte) (i >> 0);
        return bytes;
    }

    //将字节数组转换成正数
    public static int byteArrr2Int(byte[] arr) {
        int a = arr[0] << 24;
        int b = (arr[1] & 0xff) << 16;
        int c = (arr[2] & 0xff) << 8;
        int d = (arr[3] & 0xff) << 0;
        return a|b|c|d;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值