001 /*
002 * Copyright 2010 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package com.hs.mail.util;
017
018 import java.io.BufferedInputStream;
019 import java.io.BufferedOutputStream;
020 import java.io.File;
021 import java.io.FileInputStream;
022 import java.io.FileOutputStream;
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.io.OutputStream;
026 import java.util.zip.GZIPInputStream;
027 import java.util.zip.GZIPOutputStream;
028
029 import org.apache.commons.io.FilenameUtils;
030 import org.apache.commons.io.IOUtils;
031 import org.apache.commons.lang.builder.EqualsBuilder;
032
033 /**
034 *
035 * @author Won Chul Doh
036 * @since Sep 27, 2010
037 *
038 */
039 public class FileUtils {
040
041 public final static byte[] GZIP_MAGIC_BYTES = new byte[] { (byte) 0x1F,
042 (byte) 0x8B };
043
044 public final static String GZIP_EXTENSION = "zip";
045
046 public static boolean startsWith(File file, byte[] magic) {
047 InputStream input = null;
048 try {
049 int magicLen = magic.length;
050 byte[] bytes = new byte[magicLen];
051 input = new FileInputStream(file);
052 input.read(bytes, 0, magicLen);
053 return new EqualsBuilder().append(magic, bytes).isEquals();
054 } catch (IOException e) {
055 return false;
056 } finally {
057 IOUtils.closeQuietly(input);
058 }
059 }
060
061 public static boolean isCompressed(File file, boolean checkMagic) {
062 return (checkMagic) ? startsWith(file, GZIP_MAGIC_BYTES)
063 : GZIP_EXTENSION.equalsIgnoreCase(FilenameUtils
064 .getExtension(file.getName()));
065 }
066
067 public static boolean createNewFile(File file) {
068 try {
069 return file.createNewFile();
070 } catch (IOException e) {
071 return false;
072 }
073 }
074
075 public static void compress(File srcFile, File destFile) throws IOException {
076 InputStream input = null;
077 OutputStream output = null;
078 try {
079 input = new BufferedInputStream(new FileInputStream(srcFile));
080 output = new GZIPOutputStream(new FileOutputStream(destFile));
081 IOUtils.copyLarge(input, output);
082 } finally {
083 IOUtils.closeQuietly(output);
084 IOUtils.closeQuietly(input);
085 }
086 }
087
088 public static void uncompress(File srcFile, File destFile)
089 throws IOException {
090 InputStream input = null;
091 OutputStream output = null;
092 try {
093 input = new GZIPInputStream(new FileInputStream(srcFile));
094 output = new BufferedOutputStream(new FileOutputStream(destFile));
095 IOUtils.copyLarge(input, output);
096 } finally {
097 IOUtils.closeQuietly(output);
098 IOUtils.closeQuietly(input);
099 }
100 }
101
102 public static void main(String[] args) throws Exception {
103 if (args.length != 3) {
104 System.exit(0);
105 } else if ("-d".equals(args[0])) {
106 uncompress(new File(args[1]), new File(args[2]));
107 } else if ("-c".equals(args[0])) {
108 compress(new File(args[1]), new File(args[2]));
109 } else {
110 System.exit(0);
111 }
112 }
113
114 }