package P1_CommandLineFileManager_20115314;
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class CommandLineFileManager
{
private static final Exception Exception = null;
private File aFile;
private String strCommand;
private BufferedReader aBR=new BufferedReader(new InputStreamReader(System.in));
private long cutsize=1024;
private void acceptCommand(File pFile)
{
aFile=pFile;
System.out.print(aFile.getAbsolutePath()+":");
try{
String temp=aBR.readLine();
strCommand=temp.trim();
}
catch(IOException e)
{
System.out.println("Read command fail");
}
carryoutCommand(strCommand);
}
private void carryoutCommand(String strCommand)
{
if(strCommand.equals("-create"))
{
command_create();
return;
}
if(strCommand.equals("-delete"))
{
command_delete();
return ;
}
if(strCommand.equals("-enter"))
{
command_enter();
return ;
}
if(strCommand.equals("-list"))
{
command_list();
return ;
}
if(strCommand.equals("-rename"))
{
command_rename();
return ;
}
if(strCommand.equals("-copy"))
{
command_copy();
return ;
}
if(strCommand.equals("-encrypt"))
{
command_encrypt();
return ;
}
if(strCommand.equals("-decrypt"))
{
command_decrypt();
return ;
}
if(strCommand.equals("-compress"))
{
command_compress();
return ;
}
if(strCommand.equals("-uncompress"))
{
command_uncompress();
return ;
}
if(strCommand.equals("-cut"))
{
command_cut();
return ;
}
if(strCommand.equals("-merge"))
{
command_merge();
return ;
}
if(strCommand.equals("-exit"))
{
return ;
}
wrong_command(strCommand);
}
private void wrong_command(String strCommand)
{
System.out.println(strCommand+" is not correct command");
acceptCommand(aFile);
}
private void command_create() //只能创建目录
{
System.out.print("Enter the directory name:");
String filename;
try{
filename=new String(aBR.readLine());
for(int i=0;i<filename.length();i++)
{
char temp=(char) filename.indexOf(i);
if(!((temp>='a'&&temp<='z')||(temp>='A'||temp<='Z')))
throw Exception;
}
filename=aFile.getAbsolutePath()+"\\"+filename;
File pFile=new File(filename);
if(pFile.exists())
throw Exception;
pFile.mkdir();
System.out.println("Create success");
}
catch(Exception e)
{
System.out.println("Create directory fall");
}
acceptCommand(aFile);
}
private void command_delete() //即能删除目录,也能删除文件,此处的file是java意义下的File
{
try{
System.out.print("Enter the file name:");
String filename;
filename=new String(aBR.readLine());
filename=aFile.getAbsolutePath()+"\\"+filename;
File pFile=new File(filename);
if(pFile.exists())
{
delete(filename);
}
else
{
throw Exception;
}
System.out.println("Delete success");
}
catch(Exception e)
{
System.out.println("Delete fall");
}
acceptCommand(aFile);
}
private void delete(String filename) throws Exception
{
File pFile=new File(filename);
if(pFile.isFile())
{
pFile.delete();
}
else
{
String[] subfilenames=pFile.list();
for(int i=0;i<subfilenames.length;i++)
{
subfilenames[i]=pFile.getAbsolutePath()+"\\"+subfilenames[i];
delete(subfilenames[i]);
}
pFile.delete();
}
}
private void command_enter()
{
System.out.print("Enter the directory name:");
String filename=aFile.getAbsolutePath();
try
{
filename=new String(aBR.readLine());
File pFile;
if(filename.contains("\\"))
{
pFile=new File(filename);
}
else
{
pFile=new File(aFile.getAbsolutePath()+File.separator+filename);
}
if(pFile.exists()&&pFile.isDirectory())
{
aFile=pFile;
}
else
{
throw Exception;
}
}catch(Exception e){
System.out.println("Enter directory fail");
}
acceptCommand(aFile);
}
private void command_list()
{
String strOrder="name";
try
{
System.out.print("List files ordered by:");
strOrder=new String(aBR.readLine());
listFiles(strOrder);
}
catch(Exception e)
{
System.out.println("List files fail");
}
acceptCommand(aFile);
}
private void listFiles(String strOrder) throws Exception
{
String[] filenames=aFile.list();
if(filenames.length==0)
{
System.out.println("The directory is null");
}
else
{
if(strOrder.equals("name"))
{
ArrayList<String> names=new ArrayList<String>();
for(String temp:filenames)
names.add(temp);
System.out.println(names);
}
else if(strOrder.equals("size"))
{
File[] subfiles=aFile.listFiles();
Map<Double,String> sizes=new TreeMap<Double,String>();
Double counter=new Double(0);
for(File temp:subfiles)
{
double size=getFileSize(temp);
if(sizes.get(size)!=null)
{
counter+=0.000000001; //以防大小相等的文件被掩藏
size+=counter;
}
sizes.put(new Double(size),new String(temp.getName()));
}
System.out.println(sizes.values());
}
else if(strOrder.equals("date"))
{
File[] subfiles=aFile.listFiles();
Map<Double,String> changetimes=new TreeMap<Double,String>();
Double counter=new Double(0);
for(File temp:subfiles)
{
double changetime=temp.lastModified();
if(changetimes.get(changetime)!=null)
{
counter+=0.000000001;
changetime+=counter;
}
changetimes.put(new Double(changetime),new String(temp.getName()));
}
System.out.println(changetimes.values());
}
else if(strOrder.equals("type"))
{
Map<String,String> suffixs=new TreeMap<String,String>();
Integer counter=new Integer(0);
for(String temp:filenames)
{
File tempfile=new File(temp);
int index=temp.lastIndexOf(".");
if(tempfile.isDirectory()||index<0)
{
suffixs.put(new String(temp),new String(temp));
}
else
{
counter++;
String suffix=temp.substring(index, temp.length())+counter.toString();
suffixs.put(new String(suffix),new String(temp));
}
}
System.out.println(suffixs.values());
}
else
{
throw Exception;
}
}
}
private long getFileSize(File pFile)
{
long sumlength=0;
if(pFile.isFile())
{
sumlength+=pFile.length();
}
else
{
File[] subfiles=pFile.listFiles();
for(File temp:subfiles)
{
sumlength+=getFileSize(temp);
}
}
return sumlength;
}
private void command_rename()
{
String oldFileName=aFile.getAbsolutePath(),newFileName=aFile.getAbsolutePath();
try
{
System.out.print("Enter the old name of file:");
oldFileName=new String(aBR.readLine());
System.out.print("Enter the new name of file:");
newFileName=new String(aBR.readLine());
File oldFile=new File(aFile.getAbsolutePath()+File.separator+oldFileName);
File newFile=new File(aFile.getAbsolutePath()+File.separator+newFileName);
if(oldFile.exists())
{
if(oldFile.isDirectory())
{
oldFile.renameTo(newFile);
}
else
{
int index_old=oldFileName.lastIndexOf(".");
String suffix_old=oldFileName.substring(index_old, oldFileName.length());
int index_new=newFileName.lastIndexOf(".");
String suffix_new=newFileName.substring(index_new, newFileName.length());
if(suffix_old.equals(suffix_new))
{
oldFile.renameTo(newFile);