-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
176 lines (136 loc) · 4.01 KB
/
Library.java
File metadata and controls
176 lines (136 loc) · 4.01 KB
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
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import java.awt.*;
public class Library extends JFrame {
private Component center;
private JPanel panel = new JPanel();
public static Library instace = null;
public static String mangaDepot = "./Manga";
private ArrayList<Manga> library = new ArrayList<Manga>();
private Toolbar toolbar = new Toolbar();
private Sidebar sidebar = new Sidebar();
public Library() {
instace = this;
add(panel);
setTitle("Manga Viewer");
Toolkit tooklit = Toolkit.getDefaultToolkit();
BorderLayout layout = new BorderLayout();
panel.setLayout(layout);
double scale = 1.3;
Dimension size = new Dimension(
(int)(tooklit.getScreenSize().getWidth() / scale),
(int)(tooklit.getScreenSize().getHeight() / scale)
);
retrieveManga();
center = getBookRow();
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.add(toolbar, BorderLayout.NORTH);
panel.add(sidebar, BorderLayout.WEST);
panel.add(center, BorderLayout.CENTER);
setSize(size);
panel.setBackground(new Color(168, 108, 52));
setVisible(true);
}
public Component getBookRow() {
BookRow bookRow = new BookRow(library);
JScrollPane scrollPane = new JScrollPane(bookRow, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBorder(null);
return scrollPane;
}
public void refresh() {
library.clear();
retrieveManga();
open(getBookRow());
}
//public void setTitle(String title) {
// setTitle(title);
//}
private void retrieveManga() {
try {
ArrayList<File> cbzs = Library.getFiles(new File(mangaDepot));
for(File cbz : cbzs) {
library.add(new Manga(cbz, Manga.RIGHT_TO_LEFT));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Component getCenter() {
return center;
}
public void open(Component main) {
panel.remove(center);
center = main;
panel.add(center, BorderLayout.CENTER);
panel.revalidate();
}
public static ArrayList<File> getFiles(File folder) {
if(!folder.exists()) {
folder.mkdirs();
}
ArrayList<File> files = new ArrayList<File>();
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
files.add(file);
}
}
return files;
}
public static void exportManga(String mangaName, String outputPath) {
// Get the folder mangaName inside the mangaDepot folder
File mangaFolder = new File(mangaDepot + File.separator + mangaName);
// Create a ZipOutputStream to write the zipped file to
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(outputPath + File.separator + mangaName + ".cbz"));
// Iterate over the files in the mangaFolder and add them to the zip file
for (File file : mangaFolder.listFiles()) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
byte[] bytes = Files.readAllBytes(file.toPath());
zipOutputStream.write(bytes);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void addManga(String zipLocation, String mangaName) {
try {
ZipFile zip = new ZipFile(zipLocation);
Enumeration<? extends ZipEntry> entries = zip.entries();
File manga = new File(mangaDepot + File.separator + mangaName);
if(!manga.exists()) {
manga.mkdir();
}
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File file = new File(manga, entry.getName());
if(entry.isDirectory()) {
file.mkdir();
continue;
}
InputStream input = zip.getInputStream(entry);
FileOutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
input.close();
output.close();
}
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("Finished");
}
}