package Test;
import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class FileOperate
{
private static final byte[] KEYVALUE = "fffff".getBytes();
private static final int BUFFERLEN = 512;
public static boolean DelFile(String path)
{
File file = new File(path);
if (file.isFile() && file.exists())
{
file.delete();
//System.out.println("删除成功!");
return true;
}
else
{
//System.out.println("删除失败!");
return false;
}
}
public static boolean RenameFile(String path , String oldName , String newName)
{
File file = new File(path + "\\" + oldName);
if (file.isFile() && file.exists())
{
file.renameTo(new File(path + "\\" + newName));
//System.out.println("重命名成功!");
return true;
}
else
{
//System.out.println("重命名失败!");
return false;
}
}
public static boolean CopyFile(String oldPath , String newPath)
{
try
{
InputStream in=new FileInputStream(oldPath);
OutputStream out=new FileOutputStream(newPath);
IOOperation.copy(in, out);
in.close();
out.close();
//System.out.print("操作成功!");
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
//System.out.print("操作失败!");
return false;
}
}
public static boolean Encrypt(String path) throws IOException
{
try
{
String newPath = path + ".txt";
File ifile = new File(path);
File ofile = new File(newPath);
if (!ifile.isFile() || !ifile.exists())
{
System.out.println("文件不存在或不是文件!");
return false;
}
if (!ofile.isFile() || !ofile.exists())
{
ofile.createNewFile();
}
FileInputStream in = new FileInputStream(path);
FileOutputStream out = new FileOutputStream(ofile);
int c, pos=0, keylen;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1)
{
for (int i = 0; i < c; i++)
{
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
out.close();
DelFile(path);
ofile.renameTo(new File(path));
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean Decrypt(String path) throws IOException
{
try
{
String newPath = path + ".txt";
File ifile = new File(path);
File ofile = new File(newPath);
if (!ifile.isFile() || !ifile.exists())
{
System.out.println("文件不存在或不是文件!");
return false;
}
if (!ofile.isFile() || !ofile.exists())
{
ofile.createNewFile();
}
FileInputStream in = new FileInputStream(path);
FileOutputStream out = new FileOutputStream(ofile);
int c, pos, keylen;
pos = 0;
keylen = KEYVALUE.length;
byte buffer[] = new byte[BUFFERLEN];
while ((c = in.read(buffer)) != -1)
{
for (int i = 0; i < c; i++)
{
buffer[i] ^= KEYVALUE[pos];
out.write(buffer[i]);
pos++;
if (pos == keylen)
pos = 0;
}
}
in.close();
out.close();
DelFile(path);
ofile.renameTo(new File(path));
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean Compress(String path) throws IOException
{
try
{
File file = new File(path);
if (!file.isFile() || !file.exists())
{
System.out.println("文件不存在或不是文件!");
return false;
}
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path+".zip"), Charset.forName("GBK"));
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
InputStream is = new FileInputStream(path);
int len = 0;
while ((len = is.read()) != -1)
zos.write(len);
is.close();
zos.close();
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean Decompress(String path , String newPath) throws ZipException, IOException
{
try
{
File file = new File(path);//压缩文件
if (!newPath.endsWith(File.separator))
{
newPath = newPath + File.separator;
}
if (!file.isFile() || !file.exists())
{
System.out.println("文件不存在或不是文件!");
return false;
}
ZipFile zipFile = new ZipFile(file); //实例化ZipFile,每一个zip压缩文件都可以表示为一个ZipFile
//实例化一个Zip压缩文件的ZipInputStream对象,可以利用该类的getNextEntry()方法依次拿到每一个ZipEntry对象
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null)
{
String fileName = zipEntry.getName();
File temp = new File(newPath + fileName);
if (! temp.getParentFile().exists())
temp.getParentFile().mkdirs();
OutputStream os = new FileOutputStream(temp);
//通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
InputStream is = zipFile.getInputStream(zipEntry);
int len = 0;
while ((len = is.read()) != -1)
os.write(len);
os.close();
is.close();
}
zipInputStream.close();
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}