forked from abbshr/JavaExp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileCutter.java
325 lines (296 loc) · 9.38 KB
/
FileCutter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class FileCutter {
public static void main (String[] args) throws IOException{
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class Frame extends JFrame {
public Frame() throws IOException{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenW = screenSize.width;
int screenH = screenSize.height;
int frameW = 600;
int frameH = 650;
int x = screenW / 4;
int y = screenH / 4;
setSize(frameW, frameH);
setLocationRelativeTo(null);
setTitle("文件分割器");
JTabbedPane tabPane = new JTabbedPane();
tabPane.setTabPlacement(JTabbedPane.TOP);
CutPanel cutPanel = new CutPanel();
MergePanel mergePanel = new MergePanel();
tabPane.addTab("分割", cutPanel);
tabPane.addTab("合并", mergePanel);
tabPane.setSelectedIndex(0);
add(tabPane);
}
}
class CutPanel extends JPanel {
public CutPanel() throws IOException{
setLayout(null);
Listener eventListener = new Listener();
JLabel nameLabel = new JLabel("文件名:"),
sizeLabel = new JLabel("文件大小(byte):"),
blockLabel = new JLabel("块大小:"),
numLabel = new JLabel("块数量:"),
tarLabel = new JLabel("保存目录:");
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setStringPainted(true);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(rbtn1);
buttonGroup.add(rbtn2);
buttonGroup.add(rbtn3);
buttonGroup.add(rbtn4);
buttonGroup.add(rbtnS);
add(rbtn1);
add(rbtn2);
add(rbtn3);
add(rbtn4);
add(rbtnS);
add(nameLabel);
add(sizeLabel);
add(blockLabel);
add(numLabel);
add(tarLabel);
add(btCut);
add(filename);
add(filesize);
add(btCho);
add(btTar);
add(blocksize);
add(blocknum);
add(tarPath);
add(progressbar);
filename.setBounds(200, 50, 250, 20);
filesize.setBounds(200, 90, 250, 20);
btCut.setBounds(100, 400, 100, 20);
btCho.setBounds(460, 50, 100, 20);
blocksize.setBounds(400, 260, 100, 20);
nameLabel.setBounds(100, 50, 100, 20);
sizeLabel.setBounds(100, 90, 100, 20);
blockLabel.setBounds(100, 200, 100, 20);
numLabel.setBounds(100, 300, 100, 20);
tarLabel.setBounds(100, 330, 100, 20);
rbtn1.setBounds(200, 200, 200, 20);
rbtn2.setBounds(400, 200, 200, 20);
rbtn3.setBounds(200, 230, 200, 20);
rbtn4.setBounds(400, 230, 200, 20);
rbtnS.setBounds(200, 260, 200, 20);
blocknum.setBounds(210, 300, 100, 20);
tarPath.setBounds(210, 330, 100, 20);
btTar.setBounds(320, 330, 100, 20);
progressbar.setBounds(210, 400, 300, 20);
btCut.addActionListener(eventListener);
btCho.addActionListener(eventListener);
btTar.addActionListener(eventListener);
}
JTextField filename = new JTextField(),
filesize = new JTextField(),
blocksize = new JTextField(),
blocknum = new JTextField(),
tarPath = new JTextField();
JFileChooser fileChooser = new JFileChooser();
JButton btCut = new JButton("开始切割"),
btCho = new JButton("源文件"),
btTar = new JButton("目标文件");
JRadioButton rbtn1 = new JRadioButton("1.44MB",true),
rbtn2 = new JRadioButton("1.20MB"),
rbtn3 = new JRadioButton("720KB"),
rbtn4 = new JRadioButton("360KB"),
rbtnS = new JRadioButton("自定义块的大小(单位:KB):");
JProgressBar progressbar = new JProgressBar();
int realSize, currentSize = 0;
private File targetpath = null, sourcefile = null;
private double blockBytes = 0.0;
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (rbtn1.isSelected())
blockBytes = 1.44 * 1024 * 1024;
if (rbtn2.isSelected())
blockBytes = 1.2 * 1024 * 1024;
if (rbtn3.isSelected())
blockBytes = 720 * 1024;
if (rbtn4.isSelected())
blockBytes = 360 * 1024;
if (rbtnS.isSelected()) {
blockBytes = Integer.parseInt(blocksize.getText()) * 1024;
}
if (e.getSource() == btCho) {
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
sourcefile = fileChooser.getSelectedFile();
filename.setText(sourcefile.getPath());
filesize.setText(Long.toString(sourcefile.length()));
}
return;
}
if (e.getSource() == btTar) {
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
targetpath = fileChooser.getSelectedFile();
tarPath.setText(targetpath.getPath());
if (sourcefile.length() % blockBytes != 0){
blocknum.setText(Long.toString(sourcefile.length() / (long)blockBytes + 1));
} else {
blocknum.setText(Long.toString(sourcefile.length() / (long)blockBytes));
}
}
return;
}
if (e.getSource() == btCut) {
Runnable bar = new ProgressBar();
Thread t = new Thread(bar);
t.start();
}
}
}
private class ProgressBar implements Runnable {
public void run() {
if (targetpath != null && sourcefile != null) {
if (blockBytes >= sourcefile.length())
return;
try {
FileInputStream fis = new FileInputStream(sourcefile.getPath());
byte[] b = new byte[(int)blockBytes];
realSize = fis.available();
progressbar.setMaximum(realSize);
int i = 0;
while (fis.available() > 0) {
i++;
fis.read(b);
FileOutputStream fos = new FileOutputStream(targetpath.getPath() + "/part" + i);
fos.write(b);
fos.close();
currentSize = realSize - fis.available();
progressbar.setValue(currentSize);
}
fis.close();
} catch (IOException err) {}
}
}
}
}
class MergePanel extends JPanel {
public MergePanel() throws IOException{
setLayout(null);
Listener eventListener = new Listener();
JLabel numLabel = new JLabel("文件数量"),
collectLabel = new JLabel("要合并的文件:"),
tarLabel = new JLabel("目标文件:");
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setValue(0);
progressbar.setStringPainted(true);
add(numLabel);
add(collectLabel);
add(tarLabel);
add(filenum);
add(tarfile);
add(btMerge);
add(btCho);
add(btRemove);
add(btClear);
add(area);
add(progressbar);
add(btTar);
numLabel.setBounds(100, 330, 100, 20);
collectLabel.setBounds(100, 90, 100, 20);
tarLabel.setBounds(100, 360, 100, 20);
filenum.setBounds(210, 330, 100, 20);
tarfile.setBounds(210, 360, 200, 20);
btMerge.setBounds(100, 400, 100, 20);
btCho.setBounds(470, 50, 100, 20);
btRemove.setBounds(320, 330, 100, 20);
btClear.setBounds(430, 330, 100, 20);
area.setBounds(210, 120, 300, 200);
progressbar.setBounds(210, 400, 300, 20);
btTar.setBounds(420, 360, 50, 20);
btMerge.addActionListener(eventListener);
btCho.addActionListener(eventListener);
btTar.addActionListener(eventListener);
}
JTextField filenum = new JTextField(),
tarfile = new JTextField();
JButton btMerge = new JButton("开始合并"),
btCho = new JButton("选择文件"),
btRemove = new JButton("移除"),
btClear = new JButton("清空"),
btTar = new JButton("目标文件");
JTextArea area = new JTextArea(300, 200);
JProgressBar progressbar = new JProgressBar();
JFileChooser fileChooser = new JFileChooser();
int realSize, currentSize;
private File targetfile = null;
private ArrayList<File> sourcefiles = new ArrayList<File>();
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btCho) {
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setMultiSelectionEnabled(true);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
for (int i = 0; i < fileChooser.getSelectedFiles().length; i++) {
realSize += fileChooser.getSelectedFiles()[i].length();
sourcefiles.add(fileChooser.getSelectedFiles()[i]);
area.append(fileChooser.getSelectedFiles()[i].getPath() + '\n');
}
sourcefiles.trimToSize();
filenum.setText(Long.toString(sourcefiles.size()));
}
return;
}
if (e.getSource() == btTar) {
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
targetfile = fileChooser.getSelectedFile();
tarfile.setText(targetfile.getPath());
}
return;
}
if (e.getSource() == btClear) {
area.setText("");
filenum.setText("");
sourcefiles = null;
}
if (e.getSource() == btMerge) {
Runnable bar = new ProgressBar();
Thread t = new Thread(bar);
t.start();
}
}
}
private class ProgressBar implements Runnable {
public void run() {
if (targetfile != null && sourcefiles != null) {
try {
FileOutputStream fos = new FileOutputStream(targetfile.getPath());
int i = 0;
progressbar.setMaximum(realSize);
while (i < sourcefiles.size()) {
FileInputStream fis = new FileInputStream(sourcefiles.get(i).getPath());
byte[] buf = new byte[fis.available()];
currentSize += fis.available();
progressbar.setValue(currentSize);
fis.close();
fos.write(buf);
i++;
}
fos.close();
} catch (IOException err) {}
}
}
}
}