📄 fileunion.java
字号:
j++;
}
}else{//如果选择的是文件
String diziFile=directory[i-1].toString().substring(0, directory[i-1].toString().length()-5);
if(i==directory.length || !directory[i].toString().substring(0, directory[i].toString().length()-5).equals(diziFile)){//下一个选择文件同前一文件属同一文件切割而来则pass掉
int k=0001,
nFile=0;//文件的块数
long jb=0;//块文件大小总和=sub1+sub2+....+subn=复原后文件大小
String f=(k<1000)?((k<100)?((k<10)?(".000"+k):(".00"+k)):(".0"+k)):("."+k);
while((new File(diziFile+f)).exists()){//遍历寻找0001-9999序列文件,直到下一编号文件不存在
nFile++;
jb+=(new File(diziFile+f)).length();
k++;
f=(k<1000)?((k<100)?((k<10)?(".000"+k):(".00"+k)):(".0"+k)):("."+k);
}
listString.addElement(diziFile);
fileLen.addElement(jb);
if (nFile>1)
subLen.addElement(directory[i-1].length());
else
subLen.addElement(directory[i].length());
fileLength=((Long)fileLen.lastElement()<1024*1024)?(Math.round((Long)fileLen.lastElement()/1024.0*100.0)/100.0+"KB"):(Math.round((Long)fileLen.lastElement()/1024.0/1024.0*100.0)/100.0+"MB");
subLength=((Long)subLen.lastElement()<1024*1024)?(Math.round((Long)subLen.lastElement()/1024.0*100.0)/100.0+"KB"):(Math.round((Long)subLen.lastElement()/1024.0/1024.0*100.0)/100.0+"MB");
HBModel.addRow(new Object[] {
listString.lastElement(), fileLength, nFile, subLength});
}
}
}
}
}
else if (e.getSource()==srcDel){//删除
if (src.getSelectedRows().length>0){
if(FileSet.getDel()){//如果删除可用
int getInt=JOptionPane.showConfirmDialog(this, "您确认删除所选选项吗?","请确认",JOptionPane.YES_NO_OPTION);
if (getInt==JOptionPane.YES_OPTION){
delList();
}
}else{
delList();
}
}else{
JOptionPane.showMessageDialog(this, "选项为空!!!");
}
}
else if (e.getSource()==srcAllDel){//全部删除
if (src.getRowCount()>0){
if(FileSet.getDel()){
int getInt=JOptionPane.showConfirmDialog(this, "全部选项将被删除!确认吗?","请确认",JOptionPane.YES_NO_OPTION);
if (getInt==JOptionPane.YES_OPTION){
delAllList();
}
}else{
delAllList();
}
}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--){
HBModel.removeRow(srcSelectIndex[i]);
fileLen.remove(srcSelectIndex[i]);
subLen.remove(srcSelectIndex[i]);
}
}
private void delAllList(){//删除全部选项
for (int i=src.getRowCount()-1;i>=0;i--){
HBModel.removeRow(i);
}
fileLen.clear();
subLen.clear();
}
public boolean accept(File f) {//被选择的文件夹的切割文件过滤器
try{
int k=Integer.parseInt(f.toString().substring(f.toString().length() - 4));
if (f.isFile() && k>=1 && k<=9999)
return true;
}catch (NumberFormatException e){
}
return false;
}
public void run() {// 线程
src.setEnabled(false);//合并过程中不可编辑表格
srcChoice.setEnabled(false);//添加
srcDel.setEnabled(false);//删除
srcAllDel.setEnabled(false);//全删
destChoice.setEnabled(false);//浏览
options[0].setEnabled(false);//选项模式1
options[1].setEnabled(false);//选项模式2
startCut.setEnabled(false);//开始按钮
FileSet.setSpeedEditable(false);//合并过程中不允许调速
page.setEnabledAt(0, 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);//全删
destChoice.setEnabled(true);//浏览
options[0].setEnabled(true);//选项模式1
options[1].setEnabled(true);//选项模式2
startCut.setEnabled(true);//开始按钮
FileSet.setSpeedEditable(true);//合并后允许调速
page.setEnabledAt(0, 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+"字节/刀");
fileunion(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();//合完叫一声提示用户
}
public void fileunion(String srcFile,int n,int sub,long fileLen,long subLen, String dest) {// 文件合并
subBar.setVisible(true);
subBarLabel.setText("正在合并"+srcFile);
try {// 1.声明段----------------------------------
DataOutputStream writeStream = new DataOutputStream(
new FileOutputStream(dest));// 最终合并的文件
byte[] data = new byte[FileSet.COPYLEN];// 用来中转的字节数组,合并的单位进度(B):FileSet.COPYLEN
int addValue = 0;// 进度条的增长数
for (int i = 1; i <= sub; i++) {
String fileSub = (i < 1000) ? ((i < 100) ? ((i < 10) ? (".000" + i)
: (".00" + i))
: (".0" + i))
: ("." + i);
File cutFile = new File(srcFile + fileSub);// 以下三行为初始化切块文件
subLen = cutFile.length();
DataInputStream readStream = new DataInputStream(
new FileInputStream(cutFile));
long 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());
}
// 4善后段-------------------------------------
readStream.close();
}
writeStream.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "意外错误:\n" + e.getMessage());
message.setText("合并失败!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -