📄 filecut.java
字号:
else if (e.getSource()==srcSetSub){//设置块数
if (src.getSelectedRows().length>0){
String inputValue=JOptionPane.showInputDialog(this,"请输入切割的块数(范围:2~9999):","部分设置块数",JOptionPane.QUESTION_MESSAGE );
if (inputValue!=null && Integer.parseInt(inputValue)>=2 && Integer.parseInt(inputValue)<=9999){
String subLength=new String();
int srcSelectIndex[]=src.getSelectedRows();//获得选定行的索引
for (int i=0;i<srcSelectIndex.length;i++){
myModel.setValueAt(Integer.parseInt(inputValue),srcSelectIndex[i],2);
subLen.setElementAt((Long)fileLen.elementAt(srcSelectIndex[i])/Integer.parseInt(inputValue), srcSelectIndex[i]);
subLength=((Long)subLen.elementAt(srcSelectIndex[i])<1024*1024)?(Math.round((Long)subLen.elementAt(srcSelectIndex[i])/1024.0*100.0)/100.0+"KB"):(Math.round((Long)subLen.elementAt(srcSelectIndex[i])/1024.0/1024.0*100.0)/100.0+"MB");
myModel.setValueAt(subLength,srcSelectIndex[i],3);
}
}
}else{
JOptionPane.showMessageDialog(this, "选项为空!!!");
}
}
else if (e.getSource()==srcAllSetSub){//统一设置块数
if (src.getRowCount()>0){
String inputValue=JOptionPane.showInputDialog(this,"请输入切割的块数(范围:2~9999):","统一设置块数",JOptionPane.QUESTION_MESSAGE );
if (inputValue!=null && Integer.parseInt(inputValue)>=2 && Integer.parseInt(inputValue)<=9999){
String subLength=new String();
for (int i=0;i<myModel.getRowCount();i++){
myModel.setValueAt(Integer.parseInt(inputValue),i,2);
subLen.setElementAt((Long)fileLen.elementAt(i)/Integer.parseInt(inputValue), i);
subLength=((Long)subLen.elementAt(i)<1024*1024)?(Math.round((Long)subLen.elementAt(i)/1024.0*100.0)/100.0+"KB"):(Math.round((Long)subLen.elementAt(i)/1024.0/1024.0*100.0)/100.0+"MB");
myModel.setValueAt(subLength,i,3);
}
}
}else{
JOptionPane.showMessageDialog(this, "选项为空!!!");
}
}
else if (e.getSource()==destChoice){//目标文件夹
JFileChooser chooser=new JFileChooser();
chooser.setDialogTitle("切割后存放的文件夹:");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result=chooser.showOpenDialog(this);
if(result==JFileChooser.APPROVE_OPTION){
File file=chooser.getSelectedFile();
destDirectory=file.getAbsolutePath();
dest.setText(destDirectory);
}
}
else if(e.getSource()==startCut)//开始切割
if(src.getRowCount()<=0 || destDirectory==null)
message.setText("错误:必须有源文件和存放文件夹");
else
(new Thread(this)).start();
else if(e.getSource()==quit)//退出
System.exit(0);
}
private void delList(){//删除选项
int srcSelectIndex[]=src.getSelectedRows();//获得选定行的索引 1 3 6 i=3-1=2=>6
for (int i=srcSelectIndex.length-1;i>=0;i--){
myModel.removeRow(srcSelectIndex[i]);
fileLen.remove(srcSelectIndex[i]);
subLen.remove(srcSelectIndex[i]);
}
}
private void delAllList(){//删除全部选项
for (int i=src.getRowCount()-1;i>=0;i--){
myModel.removeRow(i);
}
fileLen.clear();
subLen.clear();
}
public void run(){//线程
src.setEnabled(false);//切割过程中不可编辑表格
srcChoice.setEnabled(false);//添加
srcDel.setEnabled(false);//删除
srcAllDel.setEnabled(false);//全删
srcSetSub.setEnabled(false);//设块
srcAllSetSub.setEnabled(false);//统一设块
destChoice.setEnabled(false);//浏览
options[0].setEnabled(false);//选项模式1
options[1].setEnabled(false);//选项模式2
startCut.setEnabled(false);//开始按钮
FileSet.setSpeedEditable(false);//切割过程中不允许调速
page.setEnabledAt(1, false);//切割中不允许合并
message.setText("正准备切割,请稍等……");
barPanel.setVisible(true);
try{
String []fileList=new String[src.getRowCount()];//最终还剩下的文件列表
int []subCutter=new int[src.getRowCount()];//对应的文件块数
for(int i=0;i<src.getRowCount();i++){
fileList[i]=new String(src.getValueAt(i, 0).toString());
subCutter[i]=Integer.parseInt(src.getValueAt(i, 2).toString());
}
getFileList(fileList,subCutter,dest.getText()); //获得文件列表
}catch (IOException e){
}
src.setEnabled(true);//切割后可编辑表格
srcChoice.setEnabled(true);//添加
srcDel.setEnabled(true);//删除
srcAllDel.setEnabled(true);//全删
srcSetSub.setEnabled(true);//设块
srcAllSetSub.setEnabled(true);//统一设块
destChoice.setEnabled(true);//浏览
options[0].setEnabled(true);//选项模式1
options[1].setEnabled(true);//选项模式2
startCut.setEnabled(true);//开始按钮
FileSet.setSpeedEditable(true);//切割后允许调速
page.setEnabledAt(1, true);//切割后允许合并
barPanel.setVisible(false);
subBar.setValue(0);allBar.setValue(0);
}
public void getFileList(String []src,int []sub,String dest)throws IOException{//获得切割文件列表
StringBuffer xcDest=new StringBuffer(dest);//临时变量,用于控制目标存储目录
for(int i=0;i<src.length;i++){
if (options[0].isSelected()){//为每个源文件分配单独的存储目录
xcDest.append(src[i].substring(src[i].lastIndexOf(File.separator), src[i].lastIndexOf('.'))+"~");
while (new File(xcDest.toString()).exists()){//如果有重名目录
xcDest.append("_"+(i+1));//后面跟它的序号
}
new File(xcDest.toString()).mkdir();
}
message.setText("当前正在切割第"+(i+1)+"/"+src.length+"个文件,文件切割速度:"+FileSet.COPYLEN+"字节/刀");
filecut(src[i],i,sub[i],Long.parseLong(fileLen.get(i).toString()),
Long.parseLong(subLen.get(i).toString()),
xcDest.toString()+src[i].substring(src[i].lastIndexOf(File.separator)));
//allBar.setValue((int)(100.00/src.length*(i+1)));
if (options[0].isSelected())
xcDest.delete(xcDest.lastIndexOf(File.separator),xcDest.length());
}
message.setText("所有文件切割完毕!");
Toolkit.getDefaultToolkit().beep();//切完叫一声提示用户
}
/**fileCut(源文件[String],列表号(从0开始的)[int],块数[int],文件长度[long],每块长度[long],目标文件夹[String])
*
*/
public void filecut(String srcFile,int n,int sub,long fileLen,long subLen,String dest){//文件切割
subBarLabel.setText("正在切割"+srcFile);
try{//1.声明段----------------------------------
DataInputStream readStream=new DataInputStream(new FileInputStream(srcFile));
byte []data=new byte[FileSet.COPYLEN];//用来中转的字节数组,切割的单位进度(B):FileSet.COPYLEN
int addValue=0;//进度条的增长数
long cutByteLen;
for(int i=1;i<=sub;i++){
String fileSub=(i<1000)?(
(i<100)?((i<10)?(".000"+i):(".00"+i))
:(".0"+i))
:("."+i);
DataOutputStream writeStream=new DataOutputStream(new FileOutputStream(dest+fileSub));
cutByteLen=FileSet.COPYLEN;
//2处理段------------------------------------
while(subLen-cutByteLen>=0){//块文件长度大于基切
readStream.read(data);
writeStream.write(data);
subBar.setValue((int)(((1-(subLen-cutByteLen)/(double)subLen)*100)/(double)sub)+addValue);
allBar.setValue((int)(100.00/src.getRowCount()*n)+subBar.getValue()/src.getRowCount());
cutByteLen+=FileSet.COPYLEN;
}
//3.1扫尾段-(1)if每小块段-----(2)if总段---------
if (subLen-cutByteLen<0){//每小块的余基
int finallyByte=(int)(subLen-(cutByteLen-FileSet.COPYLEN));
readStream.read(data,0,finallyByte);
writeStream.write(data,0,finallyByte);
addValue=(int)(100.0/sub*i);
subBar.setValue(addValue);
allBar.setValue((int)(100.00/src.getRowCount()*n)+subBar.getValue()/src.getRowCount());
}
if (i==sub && fileLen%sub>0){//总文件的余基,放在最后一个块里面
if(fileLen%sub<FileSet.COPYLEN){
readStream.read(data,0,(int)(fileLen%sub));
writeStream.write(data,0,(int)(fileLen%sub));
}else{
cutByteLen=FileSet.COPYLEN;
while(fileLen%sub-cutByteLen>=0){
readStream.read(data);
writeStream.write(data);
cutByteLen+=FileSet.COPYLEN;
}
if(cutByteLen-FileSet.COPYLEN<fileLen%sub){//每小块的余基
int finallyByte=(int)(fileLen%sub-(cutByteLen-FileSet.COPYLEN));
readStream.read(data,0,finallyByte);
writeStream.write(data,0,finallyByte);
}
}
subBar.setValue(100);
allBar.setValue((int)(100.00/src.getRowCount()*n)+subBar.getValue()/src.getRowCount());
}
//4善后段-------------------------------------
writeStream.close();
}
readStream.close();
}catch(IOException e){
JOptionPane.showMessageDialog(this,"意外错误:\n"+e.getMessage());
message.setText(src+"切割失败!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -