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();
}
}
compress | decompress| read | write file and decode | encode
最新推荐文章于 2025-04-01 22:25:35 发布
