compress | decompress| read | write file and decode | encode

本文介绍了一系列用于文件压缩及对象序列化的实用方法,包括将字节数组压缩为.gz文件、从.gz文件中解压获取原始字节数组、Base64编码与解码字符串、将对象存储到磁盘文件以及从磁盘文件读取对象等。这些方法适用于Java应用程序的数据处理。
public static File compressFile(byte[] buff, String fileName)
{
if (buff == null || fileName == null)
return null;

try
{

File zipFile = new File(fileName+".gz");

GZIPOutputStream os = new GZIPOutputStream(new FileOutputStream(zipFile));

BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(buff));
int count;
byte data[] = new byte[1024];
while ((count = is.read(data, 0, 1024)) != -1)
{
os.write(data, 0, count);
}
is.close();
os.close();

return zipFile;
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}

public static byte[] decompressFile(File zipFile)
{
if (!zipFile.exists())
return null;

try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();

GZIPInputStream is = new GZIPInputStream(new FileInputStream(zipFile));

int count;
byte data[] = new byte[1024];
while ((count = is.read(data, 0, 1024)) != -1)
{
baos.write(data, 0, count);
}
is.close();

byte[] buff = baos.toByteArray();

baos.close();

return buff;
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}

public static String getBase64(String str)
{
if (str == null)
return null;

try
{
return BASE64_ENCODER.encode(str.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return null;
}


public static String reverseFromBase64(String base64Str)
{
if (base64Str == null)
return null;

try
{
return new String(BASE64_DECODER.decodeBuffer(base64Str),"UTF-8");
}
catch (IOException e)
{
e.printStackTrace();
}

return null;
}

// store an object to the file of the disk
public static void storeToDisk(Serializable object, File file)
{
java.io.ObjectOutputStream outputStream;
try
{
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();

outputStream = new java.io.ObjectOutputStream(
new java.io.FileOutputStream(file));
outputStream.writeObject(object);
outputStream.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// get an object from the file of the disk
public static Serializable getObjectFromDisk(File file)
{
java.io.ObjectInputStream inputStream;
try
{
inputStream = new java.io.ObjectInputStream(
new java.io.FileInputStream(file));
Object obj = inputStream.readObject();
inputStream.close();
return (Serializable) obj;
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/**
* Write the context into the given file.
*
* @param context
* The context.
* @param theFileName
* The file name to write to.
* @throws IOException
* Exception when create the file.
*/
public static void writeToFile(String context, String theFileName)
throws IOException
{

if (theFileName != null && !"".equals(theFileName.trim()))
{
createFilePath(theFileName);
BufferedWriter aWriter = new BufferedWriter(new FileWriter(
theFileName));
aWriter.write(context);
aWriter.close();
}
else
{
throw new IllegalArgumentException("Invalid file name!");
}
}

public static void createFilePath(String fileName)
{
createFilePath(new File(fileName));
}

public static void createFilePath(File file)
{
if (!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值