ZipBase64工具

jdk1.4

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class ZipBase64
{
  public static void main(String[] args)
    throws Exception
  {
    if ((args == null) || (5 != args.length) ||
      ((!"-encode".equals(args[0])) && (!"-decode".equals(args[0]))) ||
      (!"-i".equals(args[1])) || (!"-o".equals(args[3]))) {
      printUsage();
    }

    String operateFlag = args[0];
    String inputFileName = args[2];
    String outputFileName = args[4];

    if ("-encode".equals(operateFlag)) {
      RandomAccessFile raf = null;
      BufferedOutputStream bos = null;
      try {
        raf = new RandomAccessFile(inputFileName, "r");
        byte[] data = new byte[(int)raf.length()];
        raf.readFully(data);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(baos);
        ZipEntry en = new ZipEntry("1");
        zos.putNextEntry(en);
        zos.write(data);
        zos.closeEntry();
        zos.close();
        byte[] zipData = baos.toByteArray();
        BASE64Encoder coder = new BASE64Encoder();
        String base64Data = coder.encodeBuffer(zipData);

        bos = new BufferedOutputStream(new FileOutputStream(outputFileName));
        bos.write(base64Data.getBytes("ISO-8859-1"));
        bos.flush();
      } catch (Exception e) {
        e.printStackTrace();
        try
        {
          if (raf != null)
            raf.close();
        }
        catch (Exception localException1) {
        }
        try {
          if (bos == null) return;
          bos.close();
        }
        catch (Exception localException2)
        {
        }
      }
      finally
      {
        try
        {
          if (raf != null)
            raf.close();
        }
        catch (Exception localException3) {
        }
        try {
          if (bos != null)
            bos.close();
        }
        catch (Exception localException4)
        {
        }
      }
      try
      {
        if (bos == null) return;
        bos.close();
      }
      catch (Exception localException6)
      {
      }

    }
    else if ("-decode".equals(operateFlag)) {
      RandomAccessFile raf = null;
      BufferedOutputStream bos = null;
      try {
        raf = new RandomAccessFile(inputFileName, "r");
        byte[] data = new byte[(int)raf.length()];
        raf.readFully(data);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] str = decoder.decodeBuffer(new String(data, "ISO-8859-1"));

        ByteArrayInputStream bais = new ByteArrayInputStream(str);
        ZipInputStream zis = new ZipInputStream(bais);
        ZipEntry ze = zis.getNextEntry();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[4000];
        while (true) {
          int count = zis.read(buffer);
          if (-1 == count) {
            break;
          }
          baos.write(buffer, 0, count);
        }
        byte[] unzipData = baos.toByteArray();

        bos = new BufferedOutputStream(new FileOutputStream(outputFileName));
        bos.write(unzipData);
        bos.flush();
      }
      catch (Exception localException7) {
        try {
          if (raf != null)
            raf.close();
        }
        catch (Exception localException8) {
        }
        try {
          if (bos == null) return;
          bos.close();
        }
        catch (Exception localException9)
        {
        }
      }
      finally
      {
        try
        {
          if (raf != null)
            raf.close();
        }
        catch (Exception localException10) {
        }
        try {
          if (bos != null)
            bos.close();
        }
        catch (Exception localException11)
        {
        }
      }
      try
      {
        if (bos == null) return;
        bos.close();
      }
      catch (Exception localException13) {
      }
    }
    else {
      printUsage();
    }
  }

  private static void printUsage()
  {
    System.out.println("Usage: ");
    System.out.println("java ZipBase64 -encode -i 1.jpg -o 1.txt ");
    System.out.println("or ");
    System.out.println("java ZipBase64 -decode -i 1.txt -o 1.jpg ");
  }
}

posted @   letmedown  阅读(969)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示